This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,3 @@
{{basic data operation}}Write a function that takes two logical (boolean) values, and outputs the result of "and" and "or" on both arguments as well as "not" on the first arguments. If the programming language doesn't provide a separate type for logical values, use the type most commonly used for that purpose.
If the language supports additional logical operations on booleans such as XOR, list them as well.

View file

@ -0,0 +1,2 @@
---
note: Basic Data Operations

View file

@ -0,0 +1,4 @@
(defun logical-ops (a b)
(progn$ (cw "(and a b) = ~x0~%" (and a b))
(cw "(or a b) = ~x0~%" (or a b))
(cw "(not a) = ~x0~%" (not a))))

View file

@ -0,0 +1,22 @@
PROC print_logic = (BOOL a, b)VOID:
(
# for a 6-7 bit/byte compiler #
printf(($"a and b is "gl$, a AND b);
printf(($"a or b is "gl$, a OR b);
printf(($"not a is "gl$, NOT a);
printf(($"a equivalent to b is "gl$, a EQ b);
printf(($"a not equivalent to b is "gl$, a NE b);
# Alternatively ASCII #
printf(($"a and b is "gl$, a & b);
printf(($"a and b is "gl$, a /\ b); <!-- http://web.archive.org/web/20021207211127/http://www.bobbemer.com/BRACES.HTM -->
printf(($"a or b is "gl$, a \/ b);
printf(($"a equivalent to b "gl$, a = b);
printf(($"a not equivalent to b "gl$, a /= b);
¢ for a European 8 bit/byte charcter set eg. ALCOR or GOST ¢
printf(($"a and b is "gl$, a ∧ b);
printf(($"a or b is "gl$, a b);
printf(($"not a is "gl$, ¬ a)
printf(($"a not equivalent to b is "gl$, a ≠ b)
)

View file

@ -0,0 +1,9 @@
$ awk '{print "and:"($1&&$2),"or:"($1||$2),"not:"!$1}'
0 0
and:0 or:0 not:1
0 1
and:0 or:1 not:1
1 0
and:0 or:1 not:0
1 1
and:1 or:1 not:0

View file

@ -0,0 +1,7 @@
procedure Print_Logic(A : Boolean; B : Boolean) is
begin
Put_Line("A and B is " & Boolean'Image(A and B));
Put_Line("A or B is " & Boolean'Image(A or B));
Put_Line("A xor B is " & Boolean'Image(A xor B));
Put_Line("not A is " & Boolean'Image(not A));
end Print_Logic;

View file

@ -0,0 +1,7 @@
module AndOrNot where
open import Data.Bool
open import Data.Product
test : Bool Bool Bool × Bool × Bool
test x y = x y , x y , not x

View file

@ -0,0 +1,5 @@
function logic(a,b) {
println("a AND b: " + (a && b))
println("a OR b: " + (a || b))
println("NOT a: " + (!a))
}

View file

@ -0,0 +1,10 @@
void
out(integer a, integer b)
{
o_integer(a && b);
o_byte('\n');
o_integer(a || b);
o_byte('\n');
o_integer(!a);
o_byte('\n');
}

View file

@ -0,0 +1,5 @@
a = 1
b = 0
msgbox % "a and b is " . (a && b)
msgbox % "a or b is " . (a || b)
msgbox % "not a is " . (!a)

View file

@ -0,0 +1,6 @@
a = true
b = false
print a and b
print a or b
print a xor b
print not a

View file

@ -0,0 +1,13 @@
PROClogic(FALSE, FALSE)
PROClogic(FALSE, TRUE)
PROClogic(TRUE, FALSE)
PROClogic(TRUE, TRUE)
END
DEF PROClogic(a%, b%)
LOCAL @% : @% = 2 : REM Column width
PRINT a% " AND " b% " = " a% AND b% TAB(20);
PRINT a% " OR " b% " = " a% OR b% TAB(40);
PRINT a% " EOR " b% " = " a% EOR b% TAB(60);
PRINT " NOT " a% " = " NOT a%
ENDPROC

View file

@ -0,0 +1,5 @@
logic = { a, b |
p "a and b: #{ a && b }"
p "a or b: #{ a || b }"
p "not a: #{ not a }"
}

View file

@ -0,0 +1,7 @@
void print_logic(bool a, bool b)
{
std::cout << std::boolalpha; // so that bools are written as "true" and "false"
std::cout << "a and b is " << (a && b) << "\n";
std::cout << "a or b is " << (a || b) << "\n";
std::cout << "not a is " << (!a) << "\n";
}

View file

@ -0,0 +1,6 @@
void print_logic(int a, int b)
{
printf("a and b is %d\n", a && b);
printf("a or b is %d\n", a || b);
printf("not a is %d\n", !a);
}

View file

@ -0,0 +1,6 @@
(defn logical [a b]
(prn (str "a and b is " (and a b)))
(prn (str "a or b is " (or a b)))
(prn (str "not a is " (not a))))
(logical true false)

View file

@ -0,0 +1,9 @@
<cffunction name = "logic" hint = "Performs basic logical operations">
<cfargument name = "a" required = "yes" type = "boolean" />
<cfargument name = "a" required = "yes" type = "boolean" />
<cfoutput>
'A' AND 'B' is #a AND b#< br />
'A' OR 'B' is #a OR b#< br />
NOT 'A' is #!a#
</cfoutput>
</cffunction>

View file

@ -0,0 +1,4 @@
(defun logic (a b)
(print "a and b is") (write (and a b))
(print "a or b is" ) (write (or a b))
(print "not a is" ) (write (not a)))

View file

@ -0,0 +1,33 @@
import std.stdio;
void logic(T, U)(T lhs, U rhs) {
writefln("'%s' is of type '%s', '%s' is of type '%s';",
lhs, typeid(typeof(lhs)), rhs,typeid(typeof(rhs)));
writefln("\t'%s' AND '%s' is %s, ", lhs, rhs, lhs && rhs);
writefln("\t'%s' OR '%s' is %s, ", lhs, rhs, lhs || rhs);
writefln("\tNOT '%s' is %s.\n", lhs, !lhs);
}
class C { int value; }
void main() {
bool theTruth = true;
bool theLie = false;
real zeroReal = 0.0L;
real NaN; // D initializes floating point values to NaN
int zeroInt = 0;
real[] nullArr = null;
string emptyStr = "";
string nullStr = null;
C someC = new C;
C nullC = null;
// Note: Struct is value type in D, but composite
// so no default bool equivalent.
logic(theTruth, theLie);
logic(zeroReal, NaN);
logic(zeroInt, nullArr);
logic(nullStr, emptyStr);
logic(someC, nullC);
}

View file

@ -0,0 +1,19 @@
var a := True;
var b := False;
Print('a = ');
PrintLn(a);
Print('b = ');
PrintLn(b);
Print('a AND b: ');
PrintLn(a AND b);
Print('a OR b: ');
PrintLn(a OR b);
Print('NOT a: ');
PrintLn(NOT a);
Print('a XOR b: ');
PrintLn(a XOR b);

View file

@ -0,0 +1,26 @@
program LogicalOperations;
{$APPTYPE CONSOLE}
const
a = True;
b = False;
begin
Write('a = ');
Writeln(a);
Write('b = ');
Writeln(b);
Writeln;
Write('a AND b: ');
Writeln(a AND b);
Write('a OR b: ');
Writeln(a OR b);
Write('NOT a: ');
Writeln(NOT a);
Write('a XOR b: ');
Writeln(a XOR b);
end.

View file

@ -0,0 +1,6 @@
def logicalOperations(a :boolean, b :boolean) {
return ["and" => a & b,
"or" => a | b,
"not" => !a,
"xor" => a ^ b]
}

View file

@ -0,0 +1,6 @@
def logicalOperations(a :boolean, b :boolean) {
return ["and" => a.and(b),
"or" => a.or(b),
"not" => a.not(),
"xor" => a.xor(b)]
}

View file

@ -0,0 +1,15 @@
compare_bool = fn (A, B) {
io.format("~p and ~p = ~p~n", [A, B, A and B])
io.format("~p or ~p = ~p~n", [A, B, A or B])
io.format("not ~p = ~p~n", [A, not A])
io.format("~p xor ~p = ~p~n", [A, B, A xor B])
io.format("~n")
}
@public
run = fn () {
compare_bool(true, true)
compare_bool(true, false)
compare_bool(false, true)
compare_bool(false, false)
}

View file

@ -0,0 +1,10 @@
1> true and false.
false
2> false or true.
true
3> true xor false.
true
4> not false.
true
5> not (true and true).
false

View file

@ -0,0 +1,6 @@
procedure print_logic(integer a, integer b)
printf(1,"a and b is %d\n", a and b)
printf(1,"a or b is %d\n", a or b)
printf(1,"a xor b is %d\n", a xor b)
printf(1,"not a is %d\n", not a)
end procedure

View file

@ -0,0 +1,4 @@
1 3=~["unequal, "]?
1 1= 1_=["true is -1, "]?
0~["false is 0, "]?
'm$'a>'z@>&["a < m < z"]?

View file

@ -0,0 +1,7 @@
: logical-operators ( a b -- )
{
[ "xor is: " write xor . ]
[ "and is: " write and . ]
[ "or is: " write or . ]
[ "not is: " write drop not . ]
} 2cleave ;

View file

@ -0,0 +1,21 @@
class Main
{
static Void doOps (Bool arg1, Bool arg2)
{
echo ("$arg1 and $arg2 = ${arg1.and(arg2)}")
echo ("$arg1 or $arg2 = ${arg1.or(arg2)}")
echo ("not $arg1 = ${arg1.not}")
echo ("$arg1 xor $arg2 = ${arg1.xor(arg2)}")
}
public static Void main ()
{
[true,false].each |Bool arg1|
{
[true,false].each |Bool arg2|
{
doOps (arg1, arg2)
}
}
}
}

View file

@ -0,0 +1,6 @@
: .bool ( ? -- ) if ." true" else ." false" then ;
: logic ( a b -- ) 0<> swap 0<> swap
cr ." a = " over .bool ." b = " dup .bool
cr ." a and b = " 2dup and .bool
cr ." a or b = " over or .bool
cr ." not a = " 0= .bool ;

View file

@ -0,0 +1,19 @@
SUBROUTINE PRNLOG(A, B)
LOGICAL A, B
PRINT *, 'a and b is ', A .AND. B
PRINT *, 'a or b is ', A .OR. B
PRINT *, 'not a is ', .NOT. A
C You did not ask, but the following logical operators are also standard
C since ANSI FORTRAN 66
C =======================================================================
C This yields the same results as .EQ., but has lower operator precedence
C and only works with LOGICAL operands:
PRINT *, 'a equivalent to b is ', A .EQV. B
C This yields the same results as .NE., but has lower operator precedence
C and only works with LOGICAL operands (this operation is also commonly
C called "exclusive or"):
PRINT *, 'a not equivalent to b is ', A .NEQV. B
END

View file

@ -0,0 +1,15 @@
Logical := function(a, b)
return [ a or b, a and b, not a ];
end;
Logical(true, true);
# [ true, true, false ]
Logical(true, false);
# [ true, false, false ]
Logical(false, true);
# [ true, false, true ]
Logical(false, false);
# [ false, false, true ]

View file

@ -0,0 +1,5 @@
func printLogic(a, b bool) {
fmt.Println("a and b is", a && b)
fmt.Println("a or b is", a || b)
fmt.Println("not a is", !a)
}

View file

@ -0,0 +1,9 @@
def logical = { a, b ->
println """
a AND b = ${a} && ${b} = ${a & b}
a OR b = ${a} || ${b} = ${a | b}
NOT a = ! ${a} = ${! a}
a XOR b = ${a} != ${b} = ${a != b}
a EQV b = ${a} == ${b} = ${a == b}
"""
}

View file

@ -0,0 +1,4 @@
logical(true, true)
logical(true, false)
logical(false, false)
logical(false, true)

View file

@ -0,0 +1,6 @@
a = False
b = True
a_and_b = a && b
a_or_b = a || b
not_a = not a

View file

@ -0,0 +1,6 @@
x = value1 /= 0
y = value2 /= 0
NOTx = x == 0
xANDy = x * y
xORy = x + y /= 0
EOR = x /= y

View file

@ -0,0 +1,62 @@
invocable all
procedure main() #: sample demonstrating boolean function use
limit := 4
char2 := char(2)||char(0)
every (i := char(1 to limit)|char2) do {
write(iop := "bnot","( ",image(i)," ) = ",image(iop(i)))
every k := 3 | 10 do {
write("bistrue(",image(i),",",k,") - ", if bistrue(i,k) then "returns" else "fails")
write("bisfalse(",image(i),",",k,") - ", if bisfalse(i,k) then "returns" else "fails")
}
every (j := char(1 to limit)) & (iop := "bor"|"band"|"bxor") do
write(iop,"( ",image(i),", ",image(j)," ) = ",image(iop(i,j)))
}
end
procedure bisfalse(b,p) #: test if bit p (numbered right to left from 1) is false; return b or fails
return boolean_testbit(0,b,p)
end
procedure bistrue(b,p) #: test if bit p is true; return b or fails
return boolean_testbit(1,b,p)
end
procedure bnot(b) #: logical compliment of b (not is a reserved word)
static cs,sc
initial sc := reverse(cs := string(&cset))
if type(b) ~== "string" then runerr(103,b)
return map(b,cs,sc) # en-mass inversion through remapping ordered cset
end
procedure bor(b1,b2) #: logical or
return boolean_op(ior,b1,b2)
end
procedure band(b1,b2) #: logical or
return boolean_op(iand,b1,b2)
end
procedure bxor(b1,b2) #: logical or
return boolean_op(ixor,b1,b2)
end
procedure boolean_testbit(v,b,p) #: (internal) test if bit p is true/false; return b or fail
if not 0 <= integer(p) = p then runerr(101,p)
if type(b) ~== "string" then runerr(103,b)
if v = ishift(ord(b[-p/8-1]), -(p%8)+1) then return b
end
procedure boolean_op(iop,b1,b2) #: boolean helper
local b3,i
static z
initial z := char(0)
if type(b1) ~== "string" then runerr(103,b1)
if type(b2) ~== "string" then runerr(103,b2)
b3 := ""
every i := -1 to -max(*b1,*b2) by -1 do
b3 := char(iop(ord(b1[i]|z),ord(b2[i]|z))) || b3
return b3
end

View file

@ -0,0 +1,5 @@
printLogic := method(a,b,
writeln("a and b is ", a and b)
writeln("a or b is ", a or b)
writeln("not a is ", a not)
)

View file

@ -0,0 +1 @@
aon=: *.`+.`(-.@[)`:0

View file

@ -0,0 +1,6 @@
a=: 0 0 1 1 NB. Work on vectors to show all possible
b=: 0 1 0 1 NB. 2-bit combos at once.
a aon b
0 0 0 1
0 1 1 1
1 1 0 0

View file

@ -0,0 +1,5 @@
public static void logic(boolean a, boolean b){
System.out.println("a AND b: " + (a && b));
System.out.println("a OR b: " + (a || b));
System.out.println("NOT a: " + (!a));
}

View file

@ -0,0 +1,5 @@
function logic(a,b) {
print("a AND b: " + (a && b));
print("a OR b: " + (a || b));
print("NOT a: " + (!a));
}

View file

@ -0,0 +1,13 @@
False =0
True =not( False)
print " True ="; True, "False ="; False, "NB True here shown as -1"
print
print " a b AND OR XOR"
a =0: b =0: print " "; a; " "; b; " "; a and b; " "; a or b; " "; a xor b
a =0: b =1: print " "; a; " "; b; " "; a and b; " "; a or b; " "; a xor b
a =1: b =0: print " "; a; " "; b; " "; a and b; " "; a or b; " "; a xor b
a =1: b =1: print " "; a; " "; b; " "; a and b; " "; a or b; " "; a xor b
end

View file

@ -0,0 +1,5 @@
to logic :a :b
(print [a AND b =] and :a :b)
(print [a OR b =] or :a :b)
(print [NOT a =] not :a)
end

View file

@ -0,0 +1,3 @@
function logic(a,b)
return a and b, a or b, not a
end

View file

@ -0,0 +1,3 @@
define(`logical',
`and($1,$2)=eval($1&&$2) or($1,$2)=eval($1||$2) not($1)=eval(!$1)')
logical(1,0)

View file

@ -0,0 +1,6 @@
fn printLogic a b =
(
format "a and b is %\n" (a and b)
format "a or b is %\n" (a or b)
format "not a is %\n" (not a)
)

View file

@ -0,0 +1,5 @@
LOGIC(A,B)
WRITE !,A," AND ",B," IS ",A&B
WRITE !,A," OR ",B," IS ",A!B
WRITE !,"NOT ",A," AND ",B," IS ",'(A)&B
WRITE !,"NOT ",A," OR ",B," IS ",'(A)!B

View file

@ -0,0 +1,3 @@
And[a,b,...]
Or[a,b,...]
Not[a]

View file

@ -0,0 +1,4 @@
Xor[a, b,...]
Nand[a, b,...]
Nor[a, b,...]
Xnor[a, b,...]

View file

@ -0,0 +1,9 @@
f(a, b) := [not a, a or b, a and b];
/* to use multiple arguments, use any of these */
a and b and c and d;
a or b or c or d;
"and"(a, b, c, d);
"or"(a, b, c, d);
apply("and", [a, b, c, d]);
apply("or", [a, b, c, d]);

View file

@ -0,0 +1,8 @@
def tf(expr a) = if a: "true" else: "false" fi enddef;
def test(expr a, b) =
for o = "and", "or":
message tf(a) & " " & o & " " & tf(b);
show a scantokens(o) b;
endfor
message "not " & tf(a);
show not a enddef;

View file

@ -0,0 +1,5 @@
test(true, true);
test(false, false);
test(true, false);
test(false, true);
end

View file

@ -0,0 +1,15 @@
MODULE Logical EXPORTS Main;
FROM IO IMPORT Put;
FROM Fmt IMPORT Bool;
PROCEDURE Test(a, b: BOOLEAN) =
BEGIN
Put("a AND b is " & Bool(a AND b) & "\n");
Put("a OR b is " & Bool(a OR b) & "\n");
Put("NOT a is " & Bool(NOT a) & "\n");
END Test;
BEGIN
Test(TRUE, FALSE);
END Logical.

View file

@ -0,0 +1,6 @@
function print_logic($a, $b)
{
echo "a and b is ", $a && $b ? 'True' : 'False', "\n";
echo "a or b is ", $a || $b ? 'True' : 'False', "\n";
echo "not a is ", ! $a ? 'True' : 'False', "\n";
}

View file

@ -0,0 +1,13 @@
sub show_bool
{
return shift() ? 'true' : 'false', "\n";
}
sub test_logic
{
my ($a, $b) = @_;
print "a and b is ", show_bool $a && $b;
print "a or b is ", show_bool $a || $b;
print "not a is ", show_bool !$a;
print "a xor b is ", show_bool($a xor $b);
}

View file

@ -0,0 +1,9 @@
(de logic (A B)
(prin "A AND B is ")
(println (and A B))
(prin "A OR B is ")
(println (or A B))
(prin "A XOR B is ")
(println (xor A B))
(prin "NOT A is ")
(println (not A)) )

View file

@ -0,0 +1,4 @@
def logic(a, b):
print 'a and b:', a and b
print 'a or b:' , a or b
print 'not a:' , not a

View file

@ -0,0 +1,9 @@
logic <- function(a, b) {
print(a && b)
print(a || b)
print(! a)
}
logic(TRUE, TRUE)
logic(TRUE, FALSE)
logic(FALSE, FALSE)

View file

@ -0,0 +1,31 @@
/*REXX program to show some binary (AKA bit or logical) operations. */
x=1; y=0
/*═════════════════════════════════════════════════echo X,Y values*/
call TT 'name', "value"
call TT 'x' , x
call TT 'y' , y
/*═════════════════════════════════════════════════negate X,Y values*/
call TT 'name', "negated"
call TT 'x' , \x /*some REXXes support the ¬ char.*/
call TT 'y' , \y
/*═════════════════════════════════════════════════AND X,Y values*/
call TT 'value','value',"AND"; do x=0 to 1
do y=0 to 1; call TT x,y, x & y; end
end
/*═════════════════════════════════════════════════OR X,Y values*/
call TT 'value','value',"OR"; do x=0 to 1
do y=0 to 1; call TT x,y, x | y; end
end
/*═════════════════════════════════════════════════XOR X,Y values*/
call TT 'value','value',"XOR"; do x=0 for 2
do y=0 for 2; call TT x,y, x && y; end
end
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TT subroutine───────────────────────*/
TT: parse arg a.1,a.2,a.3,a.4; hdr=length(a.1)\==1; if hdr then say; w=7
do TT=0 to hdr; _=
do k=1 for arg(); _=_ center(a.k,w); end /*k*/
say _
a.=copies('',w)
end /*TT*/
return

View file

@ -0,0 +1,5 @@
def logic(a, b)
print 'a and b: ', a && b, "\n"
print 'a or b: ' , a || b, "\n"
print 'not a: ' , !a , "\n"
end

View file

@ -0,0 +1,7 @@
def logical(a: Boolean, b: Boolean): Unit = {
println("and: " + (a && b))
println("or: " + (a || b))
println("not: " + !a)
}
logical(true, false)

View file

@ -0,0 +1,7 @@
def logical(a: Boolean, b: Boolean): IO[Unit] = for {
_ <- putStrLn("and: " ++ (a && b).shows)
_ <- putStrLn("or: " ++ (a || b).shows)
_ <- putStrLn("not: " ++ (!a).shows)
} yield ()
logical(true, false).unsafePerformIO

View file

@ -0,0 +1,10 @@
(define (logic a b)
(display "a and b is ")
(display (and a b))
(newline)
(display "a or b is ")
(display (or a b))
(newline)
(display "not a is ")
(display (not a))
(newline))

View file

@ -0,0 +1,11 @@
|test|
test := [ :a :b |
('%1 %2 %3 = %4' % { a. 'and'. b. (a & b) }) displayNl.
('%1 %2 %3 = %4' % { a. 'or'. b. (a | b) }) displayNl.
('%1 %2 = %3' % {'not'. a. (a not) }) displayNl
].
test value: true value: true.
test value: false value: false.
test value: true value: false.
test value: false value: true.

View file

@ -0,0 +1,2 @@
a implies: b
a xor: b

View file

@ -0,0 +1,5 @@
proc logic {a b} {
puts "a and b: [expr {$a && $b}]"
puts "a or b: [expr {$a || $b}]"
puts "not a: [expr {!$a}]"
}