Logical operations: Difference between revisions

m
Fixed lang tags.
(Added Perl 6.)
m (Fixed lang tags.)
Line 10:
All the operators below work equally well on arrays of boolean types. In fact, a packed array of boolean is an array of bits, providing a direct link between logical and bitwise operations.
 
<lang ada>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;</lang>
 
=={{header|ALGOL 68}}==
 
<lang algol68>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);
 
&cent;¢ for a European 8 bit/byte charcter set eg. ALCOR or GOST &cent;¢
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 not equivalent to b is "gl$, a &ne; b)
)</lang>
)
=={{header|AutoHotkey}}==
<lang AutoHotkey>a = 1
Line 49:
msgbox % "not a is " . (!a)</lang>
=={{header|AWK}}==
<lang awk>$ awk '{print "and:"($1&&$2),"or:"($1||$2),"not:"!$1}'
<lang awk>
$ awk '{print "and:"($1&&$2),"or:"($1||$2),"not:"!$1}'
0 0
and:0 or:0 not:1
Line 88 ⟶ 87:
 
=={{header|ColdFusion}}==
<lang cfm><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></lang>
 
=={{header|Common Lisp}}==
Line 161 ⟶ 160:
 
=={{header|Erlang}}==
<lang Erlang>1> true and false.
1> true and false.
false
2> false or true.
Line 171 ⟶ 169:
true
5> not (true and true).
false</lang>
</lang>
 
=={{header|FALSE}}==
FALSE uses zero/non-zero for testing False and True. Comparison operators return -1 for True and 0 for False, which work with bitwise operators for logical operations.
<lang false>1 3=~["unequal, "]?
1 1= 1_=["true is -1, "]?
0~["false is 0, "]?
'm$'a>'z@>&["a < m < z"]?</lang>
 
=={{header|Forth}}==
Forth can use bitwise operators if the boolean values are well formed: TRUE (-1) and FALSE (0). '''0<>''' converts an ill-formed flag (zero/non-zero) to a well-formed flag (false/true).
<lang forth>: .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 ;</lang>
 
=={{header|Fortran}}==
Line 261 ⟶ 258:
Instead of a function and printing, which is unidiomatic for Haskell, here are the operations in the same style as in [[Bitwise operations]]:
 
<lang haskell>a = False
b = True
 
a_and_b = a && b
a_or_b = a || b
not_a = not a</lang>
 
=={{header|Io}}==
<lang io>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)
)</lang>
)
 
=={{header|J}}==
Line 284 ⟶ 281:
 
An example more closely following the others on this page (J is interactive so indented lines are user-entered and lines flush left are system outputs):
<lang j>and=: *.
<pre>
and=: *.
or=: +.
not=: -.
Line 293 ⟶ 289:
0 0 0 1
0 1 1 1
1 1 0 0</lang>
</pre>
 
=={{header|Java}}==
Line 315 ⟶ 310:
=={{header|Logo}}==
The boolean literals are used as words ("true and "false) when used in a program.
<lang logo>to logic :a :b
(print [a AND b =] and :a :b)
(print [a OR b =] or :a :b)
(print [NOT a =] not :a)
end</lang>
 
AND and OR may have arity greater than two if used in parentheses (and :a :b :c).
 
=={{header|M4}}==
<lang m4>define(`logical',
define(`logical',
`and($1,$2)=eval($1&&$2) or($1,$2)=eval($1||$2) not($1)=eval(!$1)')
logical(1,0)</lang>
</lang>
 
Output:
Line 336 ⟶ 329:
 
=={{header|Mathematica}}==
<lang Mathematica>And[a,b,...]
AndOr[a,b,...]
Not[a]</lang>
Or[a,b,...]
Not[a]
</lang>
And can also be given using the infix operator &&, Or can also be used using the infix operator ||. Not[a] can also be written as !a.
Furthermore Mathematica supports:
<lang Mathematica>Xor[a, b,...]
XorNand[a, b,...]
NandNor[a, b,...]
NorXnor[a, b,...]</lang>
Xnor[a, b,...]
</lang>
Note that the functions are not restricted to 2 arguments; any number of arguments are allowed (except for the function Not).
All these functions can also be used with infix operators, the characters for that are: \[Xor], \[Nand], \[Nor], and \[Xnor]. Or by typing [escape] [name boolean operator] [escape].
 
=={{header|MAXScript}}==
<lang maxscript>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)
)</lang>
)
 
=={{header|Metafont}}==
Line 418 ⟶ 407:
 
=={{header|Pascal}}==
<lang pascal>procedure printlogic(a, b: boolean);
procedure printlogic(a, b: boolean);
begin
writeln('a and b is ', a and b);
writeln('a or b is ', a or b);
writeln('not a is', not a);
end;</lang>
</lang>
 
=={{header|Perl}}==
Line 460 ⟶ 447:
=={{header|Pop11}}==
 
<lang pop11>define print_logic(a, b);
<pre>
define print_logic(a, b);
printf(a and b, 'a and b is %p\n');
printf(a or b, 'a or b is %p\n');
printf(not(a), 'not a is %p\n');
enddefine;</lang>
</pre>
 
Example usage is:
<lang pop11>print_logic(true, false);</lang>
<pre>
print_logic(true, false);
</pre>
 
=={{header|PowerShell}}==
Line 525 ⟶ 508:
=={{header|Slate}}==
<lang slate>{#/\. #\/. #not} do: [ |:func|
{#/\. #\/. #not} do: [ |:func|
func arity = 1 ifTrue: [inform: 'True ' ; (func as: String) ; ' = ' ; (func sendTo: {True}) printString.
inform: 'False ' ; (func as: String) ; ' = ' ; (func sendTo: {False}) printString.].
Line 534 ⟶ 516:
[ |:each| inform: each first printString ; (func as: String) ; each second printString ; ' = ' ; (func sendTo: each) printString]]
 
].</lang>
 
</lang>
 
Output:
 
<lang slate>True/\True = True
True/\True = True
True/\False = False
False/\True = False
Line 550 ⟶ 529:
False\/False = False
True not = False
False not = True</lang>
</lang>
 
=={{header|Smalltalk}}==
Line 586 ⟶ 564:
that are the same as the well-formed flags in Forth.
 
<lang toka>[ 0 <> [ ." true" ] [ ." false"] ifTrueFalse ] is .bool
[ ( a b -- )
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
] is logic</lang>
 
=={{header|V}}==
Using stack shuffles.
 
<lang v>[mylogic
[get2 [dup] dip swap [dup] dip].
get2 and puts
get2 or puts
swap not puts
pop
].</lang>
].
 
Using view.
<lang v>[mylogic
[get2 [a b : a b a b] view].
get2 and puts
get2 or puts
swap not puts
pop
].</lang>
].
 
Using internal defines
 
<lang v>[mylogic [a b] let
a b and puts
a b or puts
a not puts
].</lang>
].
 
=={{header|Visual Basic .NET}}==
 
<lang vbnet>Function Test(ByVal a As Boolean, ByVal b As Boolean)
Console.WriteLine("And " & a And b)
Console.WriteLine("Or " & a Or b)
Console.WriteLine("Not " & Not a)
Console.WriteLine("Xor " & a Xor b)
Console.WriteLine("And, short-circuited " & a AndAlso b)
Console.WriteLine("Or, short-circuited " & a OrElse b)
End Function</lang>
 
=={{header|XSLT}}==
<lang xml><xsl:template name="logic">
<xsl:param name="a" select="true()"/>
<xsl:param name="b" select="false()"/>
<fo:block>a and b = <xsl:value-of select="$a and $b"/></fo:block>
<fo:block>a or b = <xsl:value-of select="$a or $b"/></fo:block>
<fo:block>not a = <xsl:value-of select="not($a)"/></fo:block></lang>
</xsl:template>
Anonymous user