Logical operations: Difference between revisions
→{{header|langur}}
(Logical operations en Verilog) |
Langurmonkey (talk | contribs) |
||
(39 intermediate revisions by 24 users not shown) | |||
Line 12:
=={{header|11l}}==
<
print(‘a and b: ’(a & b))
print(‘a or b: ’(a | b))
print(‘not a: ’(!a))</
=={{header|360 Assembly}}==
Line 30:
</pre>
<br>An example:
<
LOGICAL CSECT
USING LOGICAL,R15
Line 55:
PG DC CL80' '
YREGS
END LOGICAL</
{{out}}
<pre>
FALSE
</pre>
=={{header|6502 Assembly}}==
There are no built-in boolean types; however, supporting the concept in software is trivial. Typically, the zero flag or the carry flag can act as a boolean, with zero being false and nonzero being true.
<syntaxhighlight lang="6502asm">LDA myBoolean
BNE isTrue
;code that would execute if myBoolean is false, goes here.
RTS
isTrue:
;code that would execute if myBoolean is true, goes here.
RTS </syntaxhighlight>
===Branches Based On Equality to Zero===
A logical AND can easily be implemented as a nested if. Here, we'll be executing the following pseudocode. For this example, all variables are one byte in size.
<syntaxhighlight lang="c">if(myValue == 3 && myOtherValue == 5){
myResult = true;
}</syntaxhighlight>
<syntaxhighlight lang="6502asm">LDA myValue
CMP #3
BNE .skip
;if we got to here, "myValue == 3" evaluated to true.
LDA myOtherValue
CMP #5
BNE .skip
;if we got to here, both "myValue == 3" and "myOtherValue" == 5 evaluated to true.
STA myResult ;any nonzero value is considered TRUE, so we've stored 5 into myResult.
.skip:</syntaxhighlight>
A logical OR is somewhat similar.
<syntaxhighlight lang="c">if(myValue == 3 || myOtherValue == 5){
myResult = true;
}</syntaxhighlight>
<syntaxhighlight lang="6502asm">LDA myValue
CMP #3
BEQ .doTheThing
;if not equal, check myOtherValue
LDA myOtherValue
CMP #5
BNE .skip
;if we got to here, either "myValue == 3" or "myOtherValue" == 5 evaluated to true.
.doTheThing:
STA myResult ;any nonzero value is considered TRUE, so we've stored 5 into myResult.
.skip:</syntaxhighlight>
Logical NOT is the easiest of all; just use the opposite branch condition.
===Using Bit Shifts===
Chances are, however, on an 8-bit computer like the 6502, rather than using an entire byte to represent a single variable, you're going to store up to 8 related booleans in a single byte. Variables such as these are often called "bit flags" and is very common for parameters that are passed to/from external hardware, such as joysticks, video display processors, or sound cards. Each bit typically represents a different yet related variable. For example, reading a one-button joystick returns 5 bits, one for the "fire button" and the 4 directions.
<i>Side note: For joysticks, it's actually more common for 0 to represent pressed and 1 to represent not pressed, but that's out of the scope of this task.</i>
For testing multiple bits, a simple <code>BNE</code> or <code>BEQ</code> won't cut it, as this doesn't tell you WHICH bits are 0 or 1, only that a 1 exists/doesn't exist somewhere in the byte (which, if you need that info specifically, can be a nice shortcut.)
In this example, we'll be testing the bottom 2 bits of the 8-bit variable "Flags", and we want to test if both bits are 1.
<syntaxhighlight lang="6502asm">LDA flags
LSR ;test the rightmost bit.
BCC .skip
LSR ;test the bit just to the left of the one we tested prior.
BCC .skip
;your code for what happens when both of the bottom 2 bits are 1, goes here.
.skip:</syntaxhighlight>
===Using BIT===
If we're testing the top 2 bits of a byte (usually referred to as bit 7 or 6) then there's a special method we can use. The BIT instruction sets the N flag to bit 7 of the tested byte, and the V flag to bit 6 of the tested byte.
<syntaxhighlight lang="6502asm">BIT myBitFlags
BMI .Bit7Set
BVS .Bit6Set</syntaxhighlight>
For this reason, it's a good strategy when designing a bit flags variable to put the bits you'll be testing the most in bit 7 or 6 so that you spend less time checking them.
=={{header|ACL2}}==
<
(progn$ (cw "(and a b) = ~x0~%" (and a b))
(cw "(or a b) = ~x0~%" (or a b))
(cw "(not a) = ~x0~%" (not a))))</
<br><br>
=={{header|Action!}}==
<
IF a=0 THEN
RETURN (1)
Line 93 ⟶ 176:
OD
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Logical_operations.png Screenshot from Atari 8-bit computer]
Line 110 ⟶ 193:
providing a direct link between logical and bitwise operations.
<
begin
Put_Line("A and B is " & Boolean'Image(A and B));
Line 116 ⟶ 199:
Put_Line("A xor B is " & Boolean'Image(A xor B));
Put_Line("not A is " & Boolean'Image(not A));
end Print_Logic;</
=={{header|Agda}}==
===Short version===
<syntaxhighlight lang="agda">
module AndOrNot where
open import Data.Bool using (Bool ; false ; true ; _∧_ ; _∨_ ; not)
open import Data.Product using (_,_ ; _×_)
test : Bool → Bool → Bool × Bool × Bool
test
</syntaxhighlight>
e.g.
test true false ⇒ false , true , false
===Long version===
<syntaxhighlight lang="agda">
module AndOrNot where
-- This part is to compute the values
open import Data.Bool using (Bool ; false ; true ; _∧_ ; _∨_ ; not)
open import Data.Product using (_,_ ; _×_)
test : Bool → Bool → Bool × Bool × Bool
test a b = a ∧ b , a ∨ b , not a
-- This part is to print the result
open import Agda.Builtin.IO using (IO)
open import Agda.Builtin.Unit using (⊤)
open import Data.String using (String ; _++_)
open import Data.Bool.Show using (show)
get-and-or-not-str : Bool × Bool × Bool → String
get-and-or-not-str (t₁ , t₂ , t₃) =
"a and b: " ++ (show t₁) ++ ", " ++
"a or b: " ++ (show t₂) ++ ", " ++
"not a: " ++ (show t₃)
test-str : Bool → Bool → String
test-str a b = get-and-or-not-str (test a b)
postulate putStrLn : String → IO ⊤
{-# FOREIGN GHC import qualified Data.Text as T #-}
{-# COMPILE GHC putStrLn = putStrLn . T.unpack #-}
run : Bool → Bool → IO ⊤
run a b = putStrLn (test-str a b)
main : IO ⊤
main = run true false
--
-- This program outputs:
-- a and b: false, a or b: true, not a: false
--
</syntaxhighlight>
=={{header|Aikido}}==
<
function logic(a,b) {
println("a AND b: " + (a && b))
Line 140 ⟶ 275:
println("NOT a: " + (!a))
}
</syntaxhighlight>
=={{header|Aime}}==
<
out(integer a, integer b)
{
Line 153 ⟶ 288:
o_integer(!a);
o_byte('\n');
}</
=={{header|ALGOL 68}}==
<
(
# for a 6-7 bit/byte compiler #
Line 178 ⟶ 313:
printf(($"not a is "gl$, ¬ a)
printf(($"a not equivalent to b is "gl$, a ≠ b)
)</
=={{header|ALGOL W}}==
<
begin
Line 194 ⟶ 329:
write( a, " equ ", b, ": ", a = b );
end booleanOperations ;</
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
#include <hopper.h>
Line 227 ⟶ 362:
exit(0)
</syntaxhighlight>
{{out}}
<pre>
Line 287 ⟶ 422:
=={{header|Apex}}==
<
boolean b = false;
System.Debug('a AND b: ' + (a && b));
Line 293 ⟶ 428:
System.Debug('NOT a: ' + (!a));
System.Debug('a XOR b: ' + (a ^ b));
</syntaxhighlight>
=={{header|APL}}==
APL represents Boolean values using 1 and 0. This function takes Boolean arguments before it and after it—which may be arrays of Booleans—and returns an array consisting of arg1 AND arg2, arg1 OR arg2, NOT arg1, arg1 NAND arg2, arg1 NOR arg2, and arg1 XOR arg2, in that order.
<
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program logicoper.s */
Line 438 ⟶ 573:
</syntaxhighlight>
=={{header|Arturo}}==
<
print ["a AND b =" and? a b]
print ["a OR b =" or? a b]
Line 447 ⟶ 582:
]
logic true false</
{{out}}
Line 454 ⟶ 589:
a OR b = true
NOT a = false</pre>
=={{header|Asymptote}}==
<syntaxhighlight lang="asymptote">bool a = true;
bool b = false;
write(a & b);
write(a && b); //(with conditional evaluation of right-hand argument)
write(a | b);
write(a || b); //(with conditional evaluation of right-hand argument)
write(a ^ b);
write(!a);</syntaxhighlight>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">a = 1
b = 0
msgbox % "a and b is " . (a && b)
msgbox % "a or b is " . (a || b)
msgbox % "not a is " . (!a)</
=={{header|Avail}}==
Avail provides logical operators to cover all possibilities of a two-argument truth table. (Hence there are 12 entries below, plus the 4 ommitted for the trivial <code>a</code>, <code>b</code>, <code>true</code>, and <code>false</code> = 2^4.)
<
[
a : boolean;
Line 481 ⟶ 627:
Print: "a xor b: " ++ “a ⊕ b”; // equivalent to a ≠ b
Print: "a biconditional b: " ++ “a ↔ b”; // equivalent to a = b
];</
=={{header|AWK}}==
<
0 0
and:0 or:0 not:1
Line 492 ⟶ 638:
and:0 or:1 not:0
1 1
and:1 or:1 not:0</
=={{header|Axe}}==
<
r₁→A
r₂→B
Line 501 ⟶ 647:
Disp "OR:",(A??B)▶Dec,i
Disp "NOT:",(A?0,1)▶Dec,i
Return</
Note that unlike [[TI-83 BASIC]], the "and", "or", "xor", and "not(" tokens in Axe are bitwise operators, not logical operators.
=={{header|BASIC}}==
==={{header|BASIC256}}===
<
b = false
print a and b
print a or b
print a xor b
print not a</
==={{header|BBC BASIC}}===
<
PROClogic(FALSE, TRUE)
PROClogic(TRUE, FALSE)
Line 543 ⟶ 673:
PRINT a% " EOR " b% " = " a% EOR b% TAB(60);
PRINT " NOT " a% " = " NOT a%
ENDPROC</
{{out}}
<pre>
Line 551 ⟶ 681:
-1 AND -1 = -1 -1 OR -1 = -1 -1 EOR -1 = 0 NOT -1 = 0
</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
false = 0 and any non-zero value is true
<syntaxhighlight lang="qbasic">
120 b1 = false 'value of 0
130 b2 = true 'value of 1
140 print b1 and b2
150 print b1 or b2
160 print b1 xor b2
170 print b1 eqv b2
180 print b1 imp b2
190 print not b2</syntaxhighlight>
==={{header|Commodore BASIC}}===
In Commodore BASIC the "logical" operators are actually bitwise operators; to enable the proper semantics when they're used for logic, true expressions return -1 (all bits set) and false expressions return 0 (all bits clear).
<syntaxhighlight lang="qbasic">10 A = -1
20 B = 0
30 PRINT A AND B
40 PRINT A OR B
50 PRINT (A AND (NOT B)) OR ((NOT A) AND B)
60 PRINT NOT A</syntaxhighlight>
{{out}}
<pre>0
-1
-1
0</pre>
{{works with|Commodore BASIC 7.0}}
Commodore BASIC version 7 for the C-128 added XOR, but it's a function, and for some reason was written to accept only unsigned (16-bit) numbers.
<syntaxhighlight lang="basic">70 PRINT XOR(1, 0)</syntaxhighlight>
{{out}}
<pre>1</pre>
==={{header|GW-BASIC}}===
PC-BASIC has no Boolean type and does not implement Boolean operators.
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">100 LET FALSE = 0
110 LET TRUE = -1
120 PRINT TRUE
130 PRINT FALSE
120 PRINT TRUE AND FALSE
150 PRINT TRUE OR FALSE
160 PRINT TRUE XOR FALSE
170 PRINT TRUE EQV FALSE
180 PRINT TRUE IMP FALSE
190 PRINT NOT TRUE
200 END</syntaxhighlight>
==={{header|IS-BASIC}}===
<
110 LET B=0
120 PRINT A AND B
Line 561 ⟶ 741:
160 PRINT 15 BAND 4
170 PRINT 2 BOR 15
180 PRINT (A BOR B)-(A BAND B) ! xor</
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">120 b1 = false 'value of 0
130 b2 = not false 'value of -1
140 print b1 and b2
150 print b1 or b2
160 print b1 xor b2
170 print b1 eqv b2
180 print b1 imp b2
190 print not b2</syntaxhighlight>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
No booleans in BASIC... these are integers. -1 for True 0 for False.
<
b2 = 0
PRINT b1 AND b2
PRINT b1 OR b2
PRINT NOT b1</
==={{header|Yabasic}}===
<
b2 = false //value of 0
print b1 and b2
print b1 or b2
print not b1</
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<
PRINT a AND b
PRINT a OR b
PRINT NOT a
END SUB</
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">120 LET b1 = 0
130 LET b2 = -1
140 PRINT b1 AND b2
150 PRINT b1 OR b2</syntaxhighlight>
==={{header|FreeBASIC}}===
Line 603 ⟶ 800:
The following program illustrates the use of these operators:
<
Sub logicalDemo(b1 As Boolean, b2 As Boolean)
Line 629 ⟶ 826:
logicalDemo b1, b2
Print "Press any key to quit"
Sleep</
{{out}}
Line 681 ⟶ 878:
POSIX bc has neither Boolean values nor built-in logical operations.
Thus one has to write them oneself:
<
/* And */
Line 705 ⟶ 902:
"not a: "
n(a)
}</
{{Works with|GNU bc}}
GNU bc's extensions make this task much easier:
<
print "a and b: ", a && b, "\n"
print "a or b: ", a || b, "\n"
print "not a: ", !a, "\n"
}</
=={{header|Binary Lambda Calculus}}==
Minimal definitions of the logical operations in lambda calculus are: and = <code>\a\b.a b a</code>, or = <code>\a\b.a a b</code>, not = <code>\b\x\y.b y x</code>. In BLC these are <code>00 00 01 01 110 10 110</code>, or = <code>00 00 01 01 110 110 10</code>, not = <code>00 00 00 01 01 1110 10 110</code> respectively.
=={{header|BQN}}==
BQN has four logical operators: AND (`∧`), OR (`∨`), NOT (`¬`), XOR (`≠`). The function <code>L</code> lists each of those results in the same order.
<syntaxhighlight lang="bqn"> L←∧∾∨∾¬∾≠
∧∾∨∾¬∾≠
0 L 1
⟨ 0 1 0 1 ⟩</syntaxhighlight>
=={{header|Bracmat}}==
Line 722 ⟶ 930:
In the example below, the empty string represents 'true' and <code>~</code> represents 'false'. The binary operators <code>&</code> and <code>|</code>, which normally are used as the glue between expressions such as match operations, function definitions and function calls, are used as the logical operators 'and' and 'or', respectively.
<
= x y
. '$arg:(=?x,?y)
Line 748 ⟶ 956:
& out$(Logic$(,~))
& out$(Logic$(~,~))
);</
{{out}}
<pre>(x,y)=(,):
Line 771 ⟶ 979:
=={{header|Brat}}==
<
p "a and b: #{ a && b }"
p "a or b: #{ a || b }"
p "not a: #{ not a }"
}</
=={{header|C}}==
<
{
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);
}</
=={{header|C sharp|C#}}==
<
namespace LogicalOperations
Line 802 ⟶ 1,010:
}
}
}</
=={{header|C++}}==
<
{
std::cout << std::boolalpha; // so that bools are written as "true" and "false"
Line 812 ⟶ 1,020:
std::cout << "a or b is " << (a || b) << "\n";
std::cout << "not a is " << (!a) << "\n";
}</
=={{header|Clipper}}==
<
// a and b was defined as .F. (false) or .T. (true)
? a .AND. b
Line 821 ⟶ 1,029:
? .NOT. a, .NOT. b
Return Nil
</syntaxhighlight>
=={{header|Clojure}}==
<
(defn logical [a b]
(prn (str "a and b is " (and a b)))
Line 831 ⟶ 1,039:
(logical true false)
</syntaxhighlight>
=={{header|COBOL}}==
Logical operations in COBOL are exactly the same as [[Bitwise operations#COBOL|bitwise operations]].
<
PROGRAM-ID. print-logic.
Line 860 ⟶ 1,068:
GOBACK
.</
=={{header|ColdFusion}}==
<
<cfargument name = "a" required = "yes" type = "boolean" />
<cfargument name = "a" required = "yes" type = "boolean" />
Line 871 ⟶ 1,079:
NOT 'A' is #!a#
</cfoutput>
</cffunction></
=={{header|Common Lisp}}==
<
(mapcar (lambda (op)
(
(loop for a in '(nil t) do
(format t "NOT ~a is ~a~%" a (not a))
(loop for b in '(nil t) do (demo-logic a b) (terpri)))
</syntaxhighlight>
{{Out}}
<pre>NOT NIL is T
NIL AND NIL is NIL
NIL OR NIL is NIL
NIL AND T is NIL
NIL OR T is T
NOT T is NIL
T AND NIL is NIL
T OR NIL is T
T AND T is T
T OR T is T</pre>
CLISP has <tt>xor</tt>, which can be added to the list of ops in <tt>demo-logic</tt> if using that implementation, but it's not part of the standard.
=={{header|D}}==
<
void logic(T, U)(T lhs, U rhs) {
Line 913 ⟶ 1,143:
logic(nullStr, emptyStr);
logic(someC, nullC);
}</
{{out}}
<pre>'true' is of type 'bool', 'false' is of type 'bool';
Line 941 ⟶ 1,171:
=={{header|Dc}}==
<
[ 0=T 0 ] s!
Line 961 ⟶ 1,191:
0 1 lF x
1 0 lF x
1 1 lF x</
{{out}}
<pre>
Line 972 ⟶ 1,202:
=={{header|Delphi}}==
Delphi supports all logical operators shown in [[#Pascal|§ Pascal]].
Furthermore, the exclusive or operator <tt>xor</tt> is supported:
<syntaxhighlight lang="delphi"> { exclusive or }
writeLn(A:5, ' xor', B:6, ' yields', A xor B:7);</syntaxhighlight>
Beware: In Delphi the operators <tt>and</tt>, <tt>or</tt> and <tt>xor</tt> can also refer to [[Bitwise operations#Delphi|bitwise operations]].
=={{header|DWScript}}==
<
var b := False;
Line 1,027 ⟶ 1,227:
Print('a XOR b: ');
PrintLn(a XOR b);</
{{out}}
<pre>a = True
Line 1,038 ⟶ 1,238:
=={{header|Dyalect}}==
<
var b = false
print("a and b is \(a && b)")
print("a or b is \(a || b)")
print("Not a is \(!a)")</syntaxhighlight>
=={{header|Déjà Vu}}==
<
!.( a b or a b and a b xor a b not a )
for a in [ false true ]:
for b in [ false true ]:
showbool a b</
{{out}}
<pre>true true true true false false
Line 1,060 ⟶ 1,259:
=={{header|E}}==
<
return ["and" => a & b,
"or" => a | b,
"not" => !a,
"xor" => a ^ b]
}</
Each of these is a method on [http://wiki.erights.org/wiki/Boolean boolean objects]; the above is precisely equivalent to:
<
return ["and" => a.and(b),
"or" => a.or(b),
"not" => a.not(),
"xor" => a.xor(b)]
}</
If the <code>:boolean</code> guards were removed, these operations would also work on other types, such as sets (& is union and | is intersection; <code>not</code> is not supported).
Line 1,080 ⟶ 1,279:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
proc
if
r1 = 1
.
if
r2 = 1
if
r3 = 1
.
print r1 & " " & r2 & " " & r3
.
</syntaxhighlight>
=={{header|ECL}}==
<syntaxhighlight lang="ecl">
LogicalOperations(BOOLEAN A,BOOLEAN B) := FUNCTION
ANDit := A AND B;
Line 1,117 ⟶ 1,318:
LogicalOperations(TRUE,TRUE);
LogicalOperations(1>2,1=1); //Boolean expressions are also valid here
</syntaxhighlight>
=={{header|Efene}}==
<
io.format("~p and ~p = ~p~n", [A, B, A and B])
io.format("~p or ~p = ~p~n", [A, B, A or B])
Line 1,136 ⟶ 1,337:
compare_bool(false, false)
}
</syntaxhighlight>
=={{header|Elena}}==
ELENA 4.x:
<
public program()
Line 1,151 ⟶ 1,352:
console.printLine("Not a is ", a.Inverted);
console.printLine("a xor b is ", a ^^ b)
}</
{{out}}
<pre>
Line 1,162 ⟶ 1,363:
=={{header|Elixir}}==
Elixir also provides three boolean operators: <code>or</code>, <code>and</code> and <code>not</code>. These operators are strict in the sense that they expect a boolean (<code>true</code> or <code>false</code>) as their first argument:
<
false
iex(2)> false or true
true
iex(3)> not false
true</
<code>or</code> and <code>and</code> are short-circuit operators. They only execute the right side if the left side is not enough to determine the result:
Besides these boolean operators, Elixir also provides <code>||</code>, <code>&&</code> and <code>!</code> which accept arguments of any type. For these operators, all values except <code>false</code> and <code>nil</code> will evaluate to true:
<
23
iex(29)> [] || false
Line 1,184 ⟶ 1,385:
true
iex(34)> ! 3.14
false</
As a rule of thumb, use <code>and</code>, <code>or</code> and <code>not</code> when you are expecting booleans. If any of the arguments are non-boolean, use <code>&&</code>, <code>||</code> and <code>!</code>.
=={{header|Elm}}==
<syntaxhighlight lang="elm">
--Open cmd and elm-repl and directly functions can be created
Line 1,205 ⟶ 1,406:
--Output will be False, True and True of type Boolean!
--end
</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun logicOperations = void by logic a, logic b
writeLine("=== input values are " + a + ", " + b + " ===")
writeLine("a and b: " + (a and b))
writeLine(" a or b: " + (a or b))
writeLine(" not a: " + (not a))
end
logicOperations(false, false)
logicOperations(false, true)
logicOperations(true, false)
logicOperations(true, true)
</syntaxhighlight>
{{out}}
<pre>
=== input values are ⊥, ⊥ ===
a and b: ⊥
a or b: ⊥
not a: ⊤
=== input values are ⊥, ⊤ ===
a and b: ⊥
a or b: ⊤
not a: ⊤
=== input values are ⊤, ⊥ ===
a and b: ⊥
a or b: ⊤
not a: ⊥
=== input values are ⊤, ⊤ ===
a and b: ⊤
a or b: ⊤
not a: ⊥
</pre>
=={{header|Erlang}}==
<
false
2> false or true.
Line 1,217 ⟶ 1,451:
true
5> not (true and true).
false</
=={{header|Euphoria}}==
<
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</
=={{header|Excel}}==
Line 1,231 ⟶ 1,465:
If the values are typed in cells A1 and B1, type in the following in cell C1
<
=CONCATENATE($A1, " AND ", $B1, " is ", AND($A1,$B1))
</syntaxhighlight>
In D1
<
=CONCATENATE($A1, " OR ", $B1, " is ", OR($A1,$B1))
</syntaxhighlight>
In E1
<
=CONCATENATE(" NOT ", $A1, " is ", NOT($A1))
</syntaxhighlight>
=={{header|F Sharp|F#}}==
<
printfn "a and b is %b" (a && b)
printfn "a or b is %b" (a || b)
printfn "Not a is %b" (not a)
// The not-equals operator has the same effect as XOR on booleans.
printfn "a exclusive-or b is %b" (a <> b)</
=={{header|Factor}}==
<
{
[ "xor is: " write xor . ]
Line 1,262 ⟶ 1,496:
[ "or is: " write or . ]
[ "not is: " write drop not . ]
} 2cleave ;</
=={{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.
<
1 1= 1_=["true is -1, "]?
0~["false is 0, "]?
'm$'a>'z@>&["a < m < z"]?</
=={{header|Fantom}}==
<
class Main
{
Line 1,295 ⟶ 1,529:
}
}
</syntaxhighlight>
=={{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).
<
: 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 ;</
=={{header|Fortran}}==
In ANSI FORTRAN 66 or later, use LOGICAL data type:
<
LOGICAL A, B
PRINT *, 'a and b is ', A .AND. B
Line 1,326 ⟶ 1,560:
C called "exclusive or"):
PRINT *, 'a not equivalent to b is ', A .NEQV. B
END</
=={{header|Free Pascal}}==
''See [[#Delphi|Delphi]]''
=={{header|Frink}}==
<syntaxhighlight lang="frink">logical[a,b] :=
{
println["$a and $b is " + (a and b)]
println["$a or $b is " + (a or b)]
println["$a xor $b is " + (a xor b)]
println["$a nand $b is " + (a nand b)]
println["$a nor $b is " + (a nor b)]
println["not $a is " + (not a)]
}</syntaxhighlight>
=={{header|FunL}}==
<
a and b = ${a and b}
a or b = ${a or b}
Line 1,336 ⟶ 1,584:
""" )
for i <- [false, true], j <- [false, true] do logical( i, j )</
{{out}}
Line 1,366 ⟶ 1,614:
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"Logical Operations", (0,0,480,270)
Boolean a, b
text ,,,,, 43
print @"In FB, the Boolean constants _true or YES = 1, _false or NO = 0"
print fn StringByPaddingToLength( @"", 39, @"-", 0 )
print @"a\tb\tand\tor\txor\tnand\tnor"
print fn StringByPaddingToLength( @"", 39, @"-", 0 )
a = NO : b = NO : print a, b, a and b, a or b, a xor b, a nand b, a nor b
a = NO : b = YES : print a, b, a and b, a or b, a xor b, a nand b, a nor b
a = YES : b = NO : print a, b, a and b, a or b, a xor b, a nand b, a nor b
a = YES : b = YES : print a, b, a and b, a or b, a xor b, a nand b, a nor b
print
print "FB also has shorthand operator expressions"
print
print @"a\tb\t&&\t||\t^^\t^&\t^|"
print fn StringByPaddingToLength( @"", 39, @"-", 0 )
a =
a =
a = YES : b = NO : print a, b, a && b, a || b, a ^^ b, a ^& b, a ^| b
a = YES : b = YES : print a, b, a && b, a || b, a ^^ b, a ^& b, a ^| b
HandleEvents
</syntaxhighlight>
<pre>
In FB, the Boolean constants _true or YES = 1, _false or NO = 0
---------------------------------------
a b and or xor nand nor
Line 1,401 ⟶ 1,656:
1 1 1 1 0 0 -1
FB also has shorthand operator expressions
---------------------------------------
a b && || ^^ ^& ^|
Line 1,412 ⟶ 1,667:
=={{header|GAP}}==
<
return [ a or b, a and b, not a ];
end;
Line 1,426 ⟶ 1,681:
Logical(false, false);
# [ false, false, true ]</
=={{header|gecho}}==
<syntaxhighlight lang
3&&4
<syntaxhighlight lang
1||2
=={{header|Genie}}==
<
/*
Logical operations in Genie
Line 1,450 ⟶ 1,705:
a:bool = bool.parse(args[1])
b:bool = bool.parse(args[2])
logicals(a, b)</
{{out}}
Line 1,460 ⟶ 1,715:
=={{header|Go}}==
<
fmt.Println("a and b is", a && b)
fmt.Println("a or b is", a || b)
fmt.Println("not a is", !a)
}</
Other operators that work on type bool are == and !=. == corresponds to the logical operation of equivalence. != corresponds to exclusive or.
Bitwise operators come into play when you have to work with byte- or bit-level data.
::<
// stackoverflow.com/questions/28432398/difference-between-some-operators-golang
import "fmt"
Line 1,502 ⟶ 1,757:
// 3 &^ 6 = 00000001 = 1
fmt.Println(3 &^ 6)
}</
=={{header|Groovy}}==
<
println """
a AND b = ${a} && ${b} = ${a & b}
Line 1,513 ⟶ 1,768:
a EQV b = ${a} == ${b} = ${a == b}
"""
}</
Program:
<
{{out}}
Line 1,547 ⟶ 1,802:
=={{header|Harbour}}==
<
// a and b was defined as .F. (false) or .T. (true)
? a .AND. b
? a .OR. b
? ! a, ! b
RETURN</
=={{header|Haskell}}==
Line 1,558 ⟶ 1,813:
Instead of a function and printing, which is unidiomatic for Haskell, here are the operations in the same style as in [[Bitwise operations]]:
<
b = True
Line 1,566 ⟶ 1,821:
a_xor_b = a /= b
a_nxor_b = a == b
a_implies_b = a <= b -- sic! </
(&&) and (||) are lazy on the second argument and therefore this operations are not symmetric:
<
False
Prelude> undefined && False
Line 1,576 ⟶ 1,831:
True
Prelude> undefined || True
*** Exception: Prelude.undefined</
(<=), (<), (>=) and (>) on the other hand are strict:
<
*** Exception: Prelude.undefined
Prelude> undefined <= True
Line 1,585 ⟶ 1,840:
*** Exception: Prelude.undefined
Prelude> undefined < False
*** Exception: Prelude.undefined</
=={{header|hexiscript}}==
<
println "a and b = " + (a && b)
println "a or b = " + (a || b)
println " not a = " + (!a)
endfun</
=={{header|HicEst}}==
No logical variables. Nonzero is true, zero is false in logical expressions:
<
y = value2 /= 0
NOTx = x == 0
xANDy = x * y
xORy = x + y /= 0
EOR = x /= y </
=={{header|HolyC}}==
<
Print("a and b is %d\n", a && b);
Print("a or b is %d\n", a || b);
Line 1,611 ⟶ 1,866:
}
PrintLogic(TRUE, FALSE);</
=={{header|Hy}}==
<
(print "a and b:" (and a b))
(print "a or b:" (or a b))
(print "not a:" (not a)))</
=={{header|Icon}} and {{header|Unicon}}==
Line 1,640 ⟶ 1,895:
This implementation uses strings as packed arrays of bits. This facilitates easy reading and writing from external sources. While string length is variable it is controlled and doesn't change under negation. The built-in integer bit operations (ior, ixor, iand, ishift) can be utilized under the covers.
<
procedure main() #: sample demonstrating boolean function use
Line 1,701 ⟶ 1,956:
b3 := char(iop(ord(b1[i]|z),ord(b2[i]|z))) || b3
return b3
end</
{{out|Partial Sample Output:}}
Line 1,720 ⟶ 1,975:
bxor( "\x02\x00", "\x01" ) = "\x02\x01"
...</pre>
=={{Header|Insitux}}==
Insitux treats all non-<code>null</code>/<code>false</code> values as truthy, which is illustrated by using placeholder keywords <code>:a</code> and <code>:b</code> in place of just <code>true</code> to see how the different operations process them. <code>and</code> and <code>or</code> can accept more than two arguments but this is not demonstrated here.
<syntaxhighlight lang="insitux">
(let pad (comp str (pad-right " " 10)))
(print "a b | (and a b) (or a b) (not a) (xor a b)")
(print (str* "-" 20) "+" (str* "-" 40))
(join "\n"
(for a [false :a]
b [false :b]
(... str (pad a) (pad b) "| "
(for op [and or not xor]
(pad (if (= op not) (op a) (op a b)))))))
</syntaxhighlight>
{{out}}
<pre>
a b | (and a b) (or a b) (not a) (xor a b)
--------------------+----------------------------------------
false false | false null true false
false :b | false :b true :b
:a false | false :a false :a
:a :b | true :a false false
</pre>
=={{header|Io}}==
<
writeln("a and b is ", a and b)
writeln("a or b is ", a or b)
writeln("not a is ", a not)
)</
=={{header|J}}==
J uses 0 for logical false and 1 for logical true.
<
Given boolean arguments, <code>*.</code> is logical and, <code>+.</code> is logical or, and <code>-.</code>is logical not.
Additional primary logical operators include <code>*:</code> (not-and), <code>+:</code> (not-or), <code>~:</code> (exclusive-or) and <code><:</code> (logical implication).
<syntaxhighlight lang="j">
a=: 0 0 1 1 NB. Work on vectors to show all possible
b=: 0 1 0 1 NB. 2-bit combos at once.
Line 1,742 ⟶ 2,026:
0 0 0 1
0 1 1 1
1 1 0 0</
An alternate approach, based on a probabilistic interpretation, uses <code>*</code> for logical and, <code>-.</code> for logical negation and derives the others: <code>(*&.-.)</code> for logical or, <code>(-.@*)</code> for not-and, <code>(-.@*&.-.)</code> for not-or, <code>(* *&.-. -.@*&.-.)</code> for exclusive or, and <code>(*&.-. -.)~</code> for logical implication. You get the same results for simple truth values this way, but you also get consistent treatment for values between 0 and 1.
That said, J also supports truth valued operations on the binary representations of integers. (This is the concept of "packed binary", roughly speaking). For example <code>2b10001 b.</code> is '''and''', <code>2b10111 b.</code> is '''or''', <code>2b11110 b.</code> is '''nand''', etc. (the last four bits of the control argument to <code>b.</code> represent the desired binary truth table, while the prefix of that control argument in these examples specifies "packed binary"). Thus:
<syntaxhighlight lang="j"> (2b10001 b. table/~i.4);(2b10110 b. table/~i.4);<2b10000 b. table/~i.4
┌───────────────┬───────────────┬───────────────┐
│┌─────┬───────┐│┌─────┬───────┐│┌─────┬───────┐│
││17 b.│0 1 2 3│││22 b.│0 1 2 3│││16 b.│0 1 2 3││
│├─────┼───────┤│├─────┼───────┤│├─────┼───────┤│
││0 │0 0 0 0│││0 │0 1 2 3│││0 │0 0 0 0││
││1 │0 1 0 1│││1 │1 0 3 2│││1 │0 0 0 0││
││2 │0 0 2 2│││2 │2 3 0 1│││2 │0 0 0 0││
││3 │0 1 2 3│││3 │3 2 1 0│││3 │0 0 0 0││
│└─────┴───────┘│└─────┴───────┘│└─────┴───────┘│
└───────────────┴───────────────┴───────────────┘</syntaxhighlight>
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn logical_operations(anon a: bool, anon b: bool) {
println("a and b is {}", a and b)
println("a or b is {}", a or b)
println("not a is {}", not a)
}
fn main() {
let a = true
let b = false
logical_operations(a, b)
// Extra operations
println("a equals b is {}", a == b)
println("a xor b is {}", (a ^ b) == true) // == true ensures bool
}
</syntaxhighlight>
=={{header|Java}}==
<
System.out.println("a AND b: " + (a && b));
System.out.println("a OR b: " + (a || b));
System.out.println("NOT a: " + (!a));
}</
Additionally, ^ is used for XOR and == is used for "equal to" (a.k.a. bidirectional implication).
=={{header|JavaScript}}==
<
print("a AND b: " + (a && b));
print("a OR b: " + (a || b));
print("NOT a: " + (!a));
}</
=={{header|jq}}==
Line 1,767 ⟶ 2,084:
In addition to the basic logical operators, jq has <tt>any</tt> and <tt>all</tt> filters. Versions of jq since 1.4 also have extended versions of these for working efficiently with streams.
<
"\(a) and \(b) => \(a and b)",
"\(a) or \(b) => \(a or b)",
"\(a) | not => \(a | not)",
"if \(a) then true else false end => \(if a then true else false end)" ;</
'''Example''':
<
| (false, null, {}) as $b
| logic( $a; $b )</
<div style="overflow:scroll; height:200px;">
<
false and false => false
false or false => false
Line 1,813 ⟶ 2,130:
[] or {} => true
[] | not => false
if [] then true else false end => true</
=={{header|Julia}}==
<
function exerciselogic(a::Bool, b::Bool)
Line 1,832 ⟶ 2,149:
println(exerciselogic(a, b))
end
</syntaxhighlight>
{{out}}
Line 1,850 ⟶ 2,167:
=={{header|Kotlin}}==
Similar style to FreeBASIC entry:
<syntaxhighlight lang="kotlin">
fun logicalDemo(b1: Boolean, b2: Boolean) {
println("b1
println("b2
println("
println("b1
println("b1
println("
println("not b1
println("
println("b1 && b2 = ${b1 && b2}")
println("b1 || b2 = ${b1 || b2}")
println()
}
fun main(
logicalDemo(true, true)
logicalDemo(true, false)
logicalDemo(false, true)
logicalDemo(false, false)
}</syntaxhighlight>
{{out}}
<pre>
b1
b2
non-short-circuiting operators:
b1
b1
not b1 = false
short-circuiting operators:
b1 && b2 = true
b1 || b2 = true
b1
b2
non-short-circuiting operators:
b1
b1
not b1
short-circuiting operators:
b1 && b2 = false
b1 || b2 = true
b1
b2
non-short-circuiting operators:
b1
b1
not b1 = true
short-circuiting operators:
b1 && b2 = false
b1 || b2 = true
b1
b2
non-short-circuiting operators:
b1
b1
not b1 = true
short-circuiting operators:
b1 && b2 = false
b1 || b2 = false
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{and true true true false true} -> false
{or true true true false true} -> true
{not true} -> false
</syntaxhighlight>
=={{header|langur}}==
The logical operators in langur compare the "truthiness" of the left and right operands and do not require Booleans
The operators and, or, nand, nor, and?, or?, nand?, nor?, xor?, and nxor? are short-circuiting.
Line 1,924 ⟶ 2,250:
Operators that end with ? are null propagating or "database" operators, and will return null if either operand is null. They short-circuit differently than normal operators (only if the left operand is null).
<syntaxhighlight lang
join("\n", [
"{{.a}} nxor {{.b}}: {{.a nxor .b}}",
"",
"{{.a}} nxor? {{.b}}: {{.a nxor? .b}}",
"\n",
])
}
val .tests = [
Line 1,960 ⟶ 2,288:
for .t in .tests {
write .test(.t[1], .t[2])
}</
{{out}}
Line 2,109 ⟶ 2,437:
=={{header|Lasso}}==
<
define br => '\r'
Line 2,123 ⟶ 2,451:
'NOT a: ' + !#a
br
'NOT a (using not): ' + not #a</
=={{header|Liberty BASIC}}==
Line 2,129 ⟶ 2,457:
0 = false, nonzero = true.
A true value is ANY value not zero, but is usually considered to be either "1" or "-1".
<syntaxhighlight lang="lb">
False =0
True =not( False)
Line 2,143 ⟶ 2,471:
end
</syntaxhighlight>
True =-1 False =0 NB True here shown as -1
.
Line 2,153 ⟶ 2,481:
=={{header|LIL}}==
<
set first [expr 1 == 1]
set second [expr 1 == 0]
Line 2,164 ⟶ 2,492:
}
and-or-not $first $second</
{{out}}
Line 2,174 ⟶ 2,502:
=={{header|LiveCode}}==
<
local boolOpsResult
put p1 && "AND" && p2 && "=" && merge("[[p1 and p2]]") & cr after boolOpsResult
Line 2,180 ⟶ 2,508:
put "NOT" && p1 && "=" && merge("[[not p1]]") & cr after boolOpsResult
return boolOpsResult
end boolOps</
Example
<
put boolops(bop, bop) & cr after bopResult
put boolops(bop, not bop) & cr after bopResult
Line 2,203 ⟶ 2,531:
false AND true = false
false OR true = true
NOT false = true</
=={{header|LLVM}}==
<
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 2,306 ⟶ 2,634:
}
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }</
{{out}}
<pre>a and b is 0
Line 2,323 ⟶ 2,651:
=={{header|Logo}}==
The boolean literals are used as words ("true and "false) when used in a program.
<
(print [a AND b =] and :a :b)
(print [a OR b =] or :a :b)
(print [NOT a =] not :a)
end</
AND and OR may have arity greater than two if used in parentheses (and :a :b :c).
=={{header|Lua}}==
<
function logic(a,b)
return a and b, a or b, not a
end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Def Boolean A, B
Line 2,372 ⟶ 2,700:
}
CheckIt
</syntaxhighlight>
{{out}}
<pre>
Line 2,392 ⟶ 2,720:
=={{header|M4}}==
<
`and($1,$2)=eval($1&&$2) or($1,$2)=eval($1||$2) not($1)=eval(!$1)')
logical(1,0)</
{{out}}
Line 2,403 ⟶ 2,731:
=={{header|Maple}}==
Infix and prefix operators are provided for each of <code>and</code>, <code>or</code>, <code>not</code> as well as <code>xor</code> and <code>implies</code>.
<syntaxhighlight lang="maple">
f:=proc(a,b) a and b, a or b, not a; end proc:
Line 2,410 ⟶ 2,738:
f(false,true);
f(false,false);
</syntaxhighlight>
{{out}}
<pre> true, true, false
Line 2,418 ⟶ 2,746:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Or[a,b,...]
Not[a]</
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:
<
Nand[a, b,...]
Nor[a, b,...]
Xnor[a, b,...]</
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|Maxima}}==
<
/* to use multiple arguments, use any of these */
Line 2,439 ⟶ 2,767:
"or"(a, b, c, d);
apply("and", [a, b, c, d]);
apply("or", [a, b, c, d]);</
=={{header|MAXScript}}==
<
(
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)
)</
=={{header|Metafont}}==
<
def test(expr a, b) =
for o = "and", "or":
Line 2,458 ⟶ 2,786:
endfor
message "not " & tf(a);
show not a enddef;</
<
test(false, false);
test(true, false);
test(false, true);
end</
=={{header|min}}==
{{works with|min|0.19.3}}
<
:b :a
"xor is: " print! a b xor puts!
Line 2,474 ⟶ 2,802:
"or is: " print! a b or puts!
"not is: " print! a not puts!
) :logical-operators</
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,500 ⟶ 2,828:
ReadChar
END LogicalOps.</
=={{header|Modula-3}}==
<
FROM IO IMPORT Put;
Line 2,517 ⟶ 2,845:
BEGIN
Test(TRUE, FALSE);
END Logical.</
=={{header|MUMPS}}==
<syntaxhighlight lang="mumps">
LOGIC(A,B)
WRITE !,A," AND ",B," IS ",A&B
Line 2,526 ⟶ 2,854:
WRITE !,"NOT ",A," AND ",B," IS ",'(A)&B
WRITE !,"NOT ",A," OR ",B," IS ",'(A)!B
</syntaxhighlight>
=={{header|Nanoquery}}==
{{trans|Python}}
<
println "a and b: " + (a && b)
println "a or b: " + (a && b)
println "not a: " + !a
end</
While this is translated from Python, Nanoquery does not allow any object to be treated as a boolean value. As a result, this function must be called with explicit boolean values.
{{out}}
Line 2,548 ⟶ 2,876:
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Logical operations, in Neko
*/
Line 2,563 ⟶ 2,891:
if $istrue(logical) && logical > 0 $print("true path for logical AND\n")
if $istrue(logical) || logical > 1 $print("true path for logical OR\n")
if $not(logical) $print("true path for $not(1)\n") else $print("false path for $not(1)\n")</
{{out}}
Line 2,575 ⟶ 2,903:
=={{header|Nemerle}}==
<
using System.Console;
Line 2,588 ⟶ 2,916:
Main() : void {WriteLogical(true, false)}
}</
Or, if you prefer keywords to operators import the Nemerle.English namespace to use '''and''', '''or''', and '''not'''.
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols binary
Line 2,629 ⟶ 2,957:
end lx
return
</syntaxhighlight>
{{out}}
<pre>
Line 2,654 ⟶ 2,982:
=={{header|NewLISP}}==
<
(define (logic a b)
(print "a and b is: " (and a b) "\n a or b is: " (or a b))
(print "\n not a is: " (not a)))
</syntaxhighlight>
=={{header|Nim}}==
<
echo "a and b: ", a and b
echo "a or b: ", a or b
echo "not a: ", not a
echo "a xor b: ", a xor b</
=={{header|Nu}}==
<syntaxhighlight lang="nu">
def ops [a b] {{A: $a, B: $b, "Not A": (not $a), OR: ($a or $b), AND: ($a and $b), XOR: ($a xor $b)}}
[true false] | each {[[true $in] [false $in]]} | flatten | each {ops $in.0 $in.1}
</syntaxhighlight>
{{out}}
<pre>
╭───┬───────┬───────┬───────┬───────┬───────┬───────╮
│ # │ A │ B │ Not A │ OR │ AND │ XOR │
├───┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 0 │ true │ true │ false │ true │ true │ false │
│ 1 │ false │ true │ true │ true │ false │ true │
│ 2 │ true │ false │ false │ true │ false │ true │
│ 3 │ false │ false │ true │ false │ false │ false │
╰───┴───────┴───────┴───────┴───────┴───────┴───────╯
</pre>
=={{header|Objeck}}==
<
bundle Default {
class Logic {
Line 2,681 ⟶ 3,027:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
Printf.printf "a and b is %B\n" (a && b);
Printf.printf "a or b is %B\n" (a || b);
Printf.printf "not a is %B\n" (not a)</
=={{header|Octave}}==
<
s1 = num2str(a);
s2 = num2str(b);
Line 2,704 ⟶ 3,050:
test(false, false);
test(true, false);
test(false, true);</
=={{header|Oforth}}==
<
System.Out "and = " << b1 b2 and << cr
System.Out "or = " << b1 b2 or << cr
System.Out "xor = " << b1 b2 xor << cr
System.Out "not = " << b1 not << cr ;</
=={{header|OOC}}==
Bools in ooc are just covers for C's bools and respond to the same operators.
<
logic: func (a: Bool, b: Bool) {
println()
Line 2,731 ⟶ 3,077:
logic(false, true)
}
</syntaxhighlight>
=={{header|OpenEdge/Progress}}==
The logical data type can have three values: true, false or unknown (represented by question mark).
<
i_l1 AS LOGICAL,
i_l2 AS LOGICAL
Line 2,747 ⟶ 3,093:
.
END FUNCTION.</
<
testLogical( FALSE, FALSE ) SKIP(1)
testLogical( FALSE, TRUE ) SKIP(1)
Line 2,757 ⟶ 3,103:
testLogical( ?, FALSE ) SKIP(1)
testLogical( ?, TRUE ) SKIP(1)
VIEW-AS ALERT-BOX.</
{{out}}
Line 2,797 ⟶ 3,143:
=={{header|Oz}}==
<
%% using not short-circuiting standard library functions
{Show {And A B}}
Line 2,806 ⟶ 3,152:
{Show A andthen B}
{Show A orelse B}
end</
=={{header|PARI/GP}}==
Note that the forms <code>bitand()</code>, <code>bitor()</code>, <code>bitneg()</code>, and <code>bitxor()</code> also exist. These apply the operator to each bit and do not short-circuit, unlike the below.
<
print(a&b); \\ && is the same
print(a|b); \\ || is the same
print(!a);
};</
=={{header|Pascal}}==
Nine logical operators are standard.
Since [[Boolean values#Pascal|<tt>Boolean</tt> is a built-in enumeration data type]], all relational operators except the membership operator (<tt>in</tt>) are applicable.
Moreover, [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]] support the exclusive operator <tt>xor</tt>.
<syntaxhighlight lang="pascal">function logicalOperations(A, B: Boolean): Boolean;
begin
{ logical conjunction }
writeLn(A:5, ' and', B:6, ' yields', A and B:7);
{ logical disjunction }
writeLn(A:5, ' or', B:6, ' yields', A or B:7);
{ logical negation }
writeLn(' not', A:6, ' yields', not A:7);
{ logical equivalence }
writeLn(A:5, ' =', B:6, ' yields', A = B:7);
{ negation of logical equivalence }
writeLn(A:5, ' <>', B:6, ' yields', A <> B:7);
{ relational operators }
writeLn(A:5, ' <', B:6, ' yields', A < B:7);
writeLn(A:5, ' >', B:6, ' yields', A > B:7);
writeLn(A:5, ' <=', B:6, ' yields', A <= B:7);
writeLn(A:5, ' >=', B:6, ' yields', A >= B:7);
{ fulfill task requirement of writing a function: }
logicalOperations := true
end;</syntaxhighlight>
Furthermore, [[Extended Pascal]] (ISO standard 10206) defines two additional logical operators.
The operators <tt>and_then</tt> and <tt>or_else</tt> are intended for [[Short-circuit evaluation#Pascal|short-circuit evaluation]].
However, since all actual parameters need to be evaluated ''prior'' activation of the function, it makes little sense to use/show them in the above sample code.
=={{header|Perl}}==
<
{
return shift() ? 'true' : 'false', "\n";
Line 2,838 ⟶ 3,204:
print "not a is ", show_bool !$a;
print "a xor b is ", show_bool($a xor $b);
}</
There are also <code>and</code>, <code>or</code>, and <code>not</code> operators. These are just like <code>&&</code>, <code>||</code>, and <code>!</code> (respectively) except for their precedences, which are much lower.
Line 2,849 ⟶ 3,215:
Other relational operators and maths are also valid, if you wanna get clever.
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">logicop</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">or</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #008080;">not</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">xor</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">==</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span>
Line 2,860 ⟶ 3,226:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
Line 2,872 ⟶ 3,238:
Simpler version using plain integer flags:
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">logiicop</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">or</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #008080;">not</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span> <span style="color: #008080;">xor</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span>
Line 2,883 ⟶ 3,249:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
Line 2,893 ⟶ 3,259:
1 1 1 1 0 0 1 0
</pre>
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Logical_operations
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
def logiicop var b var a
( a b a b and a b or a not a b xor a b == a b != )
enddef
def printSec
len for get print "\t" print endfor drop nl
enddef
( "a" "b" "and" "or" "not" "xor" "==" "!=" ) printSec
( 0 1 ) for dup
( 0 1 ) for
logiicop printSec
endfor
endfor</syntaxhighlight>
{{out}}
<pre>a b and or not xor == !=
0 0 0 0 1 0 1 0
0 1 0 1 1 1 0 1
1 0 0 1 0 1 0 1
1 1 1 1 0 0 1 0
=== Press any key to exit ===</pre>
=={{header|PHP}}==
<
{
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";
}</
=={{header|PicoLisp}}==
<
(prin "A AND B is ")
(println (and A B))
Line 2,912 ⟶ 3,307:
(println (xor A B))
(prin "NOT A is ")
(println (not A)) )</
=={{header|PL/I}}==
<
declare (t, u) bit (1);
Line 2,922 ⟶ 3,317:
put skip list (^t); /* logical not */
put skip list (t ^ u); /* exclusive or */
end logical_ops;</
=={{header|Pop11}}==
<
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;</
Example usage is:
<syntaxhighlight lang
=={{header|PostScript}}==
<
/logical{
/a exch def
Line 2,944 ⟶ 3,339:
a not =
}def
</syntaxhighlight>
=={{header|PowerShell}}==
<
Write-Host "A and B: " ($a -and $b)
Write-Host "A or B: " ($a -or $b)
Line 2,953 ⟶ 3,348:
Write-Host "not A: " (!$a)
Write-Host "A xor B: " ($a -xor $b)
}</
=={{header|Prolog}}==
Line 2,990 ⟶ 3,385:
=={{header|PureBasic}}==
<
Debug a & b ;And
Debug a | b ;Or
Line 2,999 ⟶ 3,394:
logicDebug(#True, #True)
logicDebug(#True, #False)
logicDebug(#False, #False)</
=={{header|Python}}==
<
print('a and b:', a and b)
print('a or b:', a or b)
print('not a:', not a)</
Note: Any normal object can be treated as a Boolean in Python. Numeric objects which evaluate to any non-zero value are "True" otherwise they are false. Non-empty strings, lists, tuples and other sequences are "True" otherwise they are false. The pre-defined ''None'' object is also treated as "False." In Python 2.3 pre-defined objects named ''True'' and ''False'' were added to the language; prior to that it was a common convention to include a line: ''False, True = 0, 1'' to use these as names. Custom classes which implement ''__nonzero__'' or ''__len__'' or some other special methods can be implicitly evaluated as Booleans based on those results.
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
Dim As _Unsigned _Bit First, Second
First = 0: Second = 1
Print " Operator F S results "
Print " AND 1 0 "; First And Second
Print " XOR 1 0 "; First Xor Second
Print " OR 1 0 "; First Or Second
Print " NOT 1 "; Not First
Print " EQV 1 0 "; First Eqv Second
Print " IMP 1 0 "; First Imp Second
</syntaxhighlight>
=={{header|Quackery}}==
Line 3,013 ⟶ 3,425:
Quackery also has the boolean words <code>nand</code> and <code>xor</code>.
<
else [ say "false"] ] is echobool ( b --> )
Line 3,021 ⟶ 3,433:
say "A or B is " echobool cr
not
say "not A is " echobool cr ] is task ( A B --> )</
{{out}}
Line 3,060 ⟶ 3,472:
=={{header|R}}==
<
print(a && b)
print(a || b)
Line 3,068 ⟶ 3,480:
logic(TRUE, TRUE)
logic(TRUE, FALSE)
logic(FALSE, FALSE)</
=={{header|Racket}}==
<
(define (logic a b)
Line 3,080 ⟶ 3,492:
(displayln (format "a nor b equals ~a" (nor a b)))
(displayln (format "a implies b equals ~a" (implies a b)))
(displayln (format "a xor b equals ~a" (xor a b))))</
=={{header|Raku}}==
Line 3,086 ⟶ 3,498:
Raku has an abundance of logical operators for various purposes.
<syntaxhighlight lang="raku"
say "$a && $b is ", $a && $b; # short-circuiting
say "$a || $b is ", $a || $b; # short-circuiting
Line 3,114 ⟶ 3,526:
}
logic(3,10);</
{{out}}
<pre>3 && 10 is 10
Line 3,138 ⟶ 3,550:
=={{header|Rascal}}==
<
public void logic(bool a, bool b){
Line 3,146 ⟶ 3,558:
println("a implies b, is <a ==> b>");
println("not a", <!a>");
}</
{{out}}
Line 3,159 ⟶ 3,571:
=={{header|REBOL}}==
<
print ['and tab a and b]
print ['or tab a or b]
Line 3,171 ⟶ 3,583:
print ['any tab any [a b]]
print ['all tab all [a b]]
]</
Example:
Line 3,188 ⟶ 3,600:
=={{header|Relation}}==
<syntaxhighlight lang="relation">
program logic(x,y)
relation a, b, op, result
Line 3,202 ⟶ 3,614:
run logic(1,0)
run logic(1,1)
</syntaxhighlight>
In Relation TRUE is the number 1 (or any different from 0) and FALSE 0.
=={{header|ReScript}}==
<
Js.log(string_of_bool(a) ++ " and " ++ string_of_bool(b) ++ " = " ++ string_of_bool(a && b))
Js.log(string_of_bool(a) ++ " or " ++ string_of_bool(b) ++ " = " ++ string_of_bool(a || b))
Line 3,220 ⟶ 3,632:
logic2(true)
logic2(false)</
{{out}}
<pre>
Line 3,238 ⟶ 3,650:
=={{header|Retro}}==
<
: logic ( ab- )
"\na = " puts over .bool "b = " puts dup .bool
"\na and b = " puts 2dup and .bool
"\na or b = " puts over or .bool
"\nnot a = " puts not .bool ;</
=={{header|REXX}}==
Line 3,254 ⟶ 3,666:
Any other value will raise a REXX '''syntax''' error condition.
===basic boolean functions===
<
x= 1 ; @x= ' x ' /*set the initial values of X and Y, */
y= 0 ; @y= ' y ' /* and a couple of literals for HDRs. */
Line 3,279 ⟶ 3,691:
@.= copies('═', 7) /*define a new header separator line. */
end /*j*/
return</
{{out|output|text= when using the default (internal) inputs:}}
<pre>
Line 3,318 ⟶ 3,730:
===extended boolean functions===
All sixteen boolean functions could easily be shown.
<
x= 1 ; @x= ' x ' /*set the initial values of X and Y, */
y= 0 ; @y= ' y ' /* and a couple of literals for HDRs. */
Line 3,348 ⟶ 3,760:
@.= copies('═', 7) /*define a new separator (header) line.*/
end /*j*/
return</
{{out|output|text= when using the default (internal) inputs:}}
<pre>
Line 3,407 ⟶ 3,819:
=={{header|Ring}}==
<
x = true
y = false
Line 3,414 ⟶ 3,826:
see "x or y = " + (x or y) + nl
see "not x = " + (not x) + nl
</syntaxhighlight>
=={{header|RLaB}}==
Line 3,420 ⟶ 3,832:
<code>and/or/not</code> are synonymous with <code>&&/||/!</code>. In the case when the argument is a real number (default type of argument) the default statement in the absence of ''if'' command is ''is the argument non-zero''.
Therefore
<syntaxhighlight lang="rlab">
>> x = 5
5
Line 3,431 ⟶ 3,843:
>> x && y
0
</syntaxhighlight>
However, if arguments to the functions are of the type ''integer'' then the functions operate bit-wise.
<syntaxhighlight lang="rlab">
>> x = int(5)
5
Line 3,445 ⟶ 3,857:
>> x && y
0
</syntaxhighlight>
=={{header|Robotic}}==
Line 3,451 ⟶ 3,863:
However, [[Bitwise_operations|bitwise operators]] can be used.
=={{header|RPL}}==
≪ → a b
≪ "a and b = " a b AND →STR +
"a or b = " a b OR →STR +
"not a = " a NOT →STR +
"a xor b = " a b XOR →STR +
≫ ≫ ''''LOGIC'''' STO
=={{header|Ruby}}==
<
print 'a and b: ', a && b, "\n"
print 'a or b: ' , a || b, "\n"
print 'not a: ' , !a , "\n"
print 'a xor b: ' , a ^ b, "\n"
end</
<code>and/or/not</code> are synonymous with <code>&&/||/!</code> albeit with lower precedence.
=={{header|Rust}}==
{{works with|Rust|1.1}}
<syntaxhighlight lang="rust">
fn boolean_ops(a: bool, b: bool) {
println!("{} and {} -> {}", a, b, a && b);
Line 3,476 ⟶ 3,895:
boolean_ops(false, false)
}
</syntaxhighlight>
The Boolean operators || and && are more efficient versions of | and & in that the right-hand operand is only evaluated when the left-hand operand does not already determine the result of the expression.
=={{header|Scala}}==
In vanilla Scala:
<
println("and: " + (a && b))
println("or: " + (a || b))
Line 3,487 ⟶ 3,906:
}
logical(true, false)</
With Scalaz:
<
_ <- putStrLn("and: " ++ (a && b).shows)
_ <- putStrLn("or: " ++ (a || b).shows)
Line 3,496 ⟶ 3,915:
} yield ()
logical(true, false).unsafePerformIO</
=={{header|Scheme}}==
<
(display "a and b is ")
(display (and a b))
Line 3,508 ⟶ 3,927:
(display "not a is ")
(display (not a))
(newline))</
=={{header|Seed7}}==
<
begin
writeln("a and b is " <& a and b);
writeln("a or b is " <& a or b);
writeln("not a is " <& not a);
end func;</
=={{header|Self}}==
<
( true && false ) = false.
( true ^^ false ) = true. "xor"
( true || false ) = true. "or"
</syntaxhighlight>
=={{header|Sidef}}==
<
say ("a and b: ", a && b);
say ("a or b: ", a || b);
Line 3,534 ⟶ 3,953:
}
logic(false, true);</
{{out}}
<pre>a and b: false
Line 3,547 ⟶ 3,966:
This makes a closure that takes two Boolean values. Booleans can be indicated by predicate identifier names that end with a question mark <code>?</code>.
<
(a? b?)
[
Line 3,558 ⟶ 3,977:
println("a not xor b: " a nxor b)
]
</syntaxhighlight>
Example call:
<syntaxhighlight lang
=={{header|Slate}}==
{{lines too long|Slate}}
<
func arity = 1 ifTrue: [inform: 'True ' ; (func as: String) ; ' = ' ; (func sendTo: {True}) printString.
inform: 'False ' ; (func as: String) ; ' = ' ; (func sendTo: {False}) printString.].
Line 3,574 ⟶ 3,993:
[ |:each| inform: each first printString ; (func as: String) ; each second printString ; ' = ' ; (func sendTo: each) printString]]
].</
{{out}}
Line 3,595 ⟶ 4,014:
There are also non-evaluating versions named "and:" and "or:", which only evaluate expr2 if the result is not already determined by expr1.
<
test := [ :a :b |
('%1 %2 %3 = %4' % { a. 'and'. b. (a & b) }) displayNl.
Line 3,605 ⟶ 4,024:
test value: false value: false.
test value: true value: false.
test value: false value: true.</
{{works with|Smalltalk/X}}
<
a xor: b</
=={{header|Standard ML}}==
<
print ("a and b is " ^ Bool.toString (a andalso b) ^ "\n");
print ("a or b is " ^ Bool.toString (a orelse b) ^ "\n");
print ("not a is " ^ Bool.toString (not a) ^ "\n")
)</
=={{header|Stata}}==
Line 3,624 ⟶ 4,043:
Stata does not have a boolean type, and uses instead 0 and 1 to denote resp. false and true.
<
args a b
di `a'&`b'
di `a'|`b'
di !`a'
end</
Likewise in Mata:
<
printf("%f\n",a&b)
printf("%f\n",a|b)
printf("%f\n",!a)
}</
=={{header|Swift}}==
<
println("a AND b: \(a && b)");
println("a OR b: \(a || b)");
println("NOT a: \(!a)");
}</
Additionally, ^ is used for XOR and == is used for "equal to" (a.k.a. bidirectional implication).
=={{header|Tcl}}==
<
puts "a and b: [expr {$a && $b}]"
puts "a or b: [expr {$a || $b}]"
puts "not a: [expr {!$a}]"
}</
=={{header|Terraform}}==
The Hashicorp Configuration Language ( HCL ) does not support user defined functions. It only supports AND, OR and NOT logical operations. HCL is not meant for generic programming but I don't see an use case for a logarithm function in a language meant to provision infrastructure either. So......
<syntaxhighlight lang="terraform">
#Aamrun, August 15th 2022
variable "a" {
type = bool
default = true
}
variable "b" {
type = bool
default = false
}
output "a_and_b" {
value = var.a && var.b
}
output "a_or_b" {
value = var.a || var.b
}
output "not_a" {
value = !var.a
}
</syntaxhighlight>
'''Invocation and output :'''
<pre>
$ terraform apply -var="a=true" -var="b=false" -auto-approve
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
a_and_b = false
a_or_b = true
not_a = false
$
</pre>
=={{header|Toka}}==
Line 3,660 ⟶ 4,125:
that are the same as the well-formed flags in Forth.
<
[ ( a b -- )
cr ." a = " over .bool ." b = " dup .bool
Line 3,666 ⟶ 4,131:
cr ." a or b = " over or .bool
cr ." not a = " 0 = .bool
] is logic</
=={{header|uBasic/4tH}}==
uBasic/4tH does not have logical operators, but every non-zero value will be considered ''TRUE'' in conditional statements. However, comparison operators (like =, #, < and >) can be used in expressions and will return fully qualified booleans. Hence, simple arithmetic operators will do the trick just fine.
<syntaxhighlight lang="text">Proc _Boolean(4, 2)
Proc _Boolean(0, 2)
Proc _Boolean(2, 0)
Line 3,685 ⟶ 4,150:
print "not A is "; a@ = 0 ' This will invert the boolean value
print
Return</
{{out}}
<pre>A and B is 1
Line 3,701 ⟶ 4,166:
0 OK, 0:63</pre>
=={{header|UNIX Shell}}==
The shell has at least two levels of logical operators. Conditional logic (<tt>if</tt>, <tt>while</tt>, <tt>&&</tt> and <tt>||</tt> at the statement level) operates on commands; the commands are executed, and their exit status determines their value in a Boolean context. If they return an exit code of 0, signaling successful execution, that is considered a "true" result; if they return a nonzero exit code, signaling a failure condition, that is considered a "false" result. However, these results are not returned as a Boolean value. <tt>if command; then do something; fi</tt> will do something if the command succeeds, but there's no "true" value, only the zero exit status. So this demo uses a function that examines the exit status of the last command and prints "true" if it is 0 and "false" otherwise. The two values for the task are the <em>commands</em> <tt>true</tt> and <tt>false</tt>, which do nothing but exit with status 0 and 1, respectively.
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{Works with|Z Shell}}
<syntaxhighlight lang="bash">function boolVal {
if (( ! $? )); then
echo true
else
echo false
fi
}
a=true
b=false
printf '%s and %s = %s\n' "$a" "$b" "$("$a" && "$b"; boolVal)"
printf '%s or %s = %s\n' "$a" "$b" "$("$a" || "$b"; boolVal)"
printf 'not %s = %s\n' "$a" "$(! "$a"; boolVal)"</syntaxhighlight>
{{Out}}
<pre>true and false = false
true or false = true
not true = false</pre>
A different variety of Boolean logic is available inside arithmetic expressions, using the C convention of 0=false and nonzero=true=1:
<syntaxhighlight lang="bash">a=1
b=0
printf '%d and %d = %d\n' "$a" "$b" "$(( a && b ))"
printf '%d or %d = %d\n' "$a" "$b" "$(( a || b ))"
printf 'not %d = %d\n' "$a" "$(( ! a ))"</syntaxhighlight>
{{Out}}
<pre>1 and 0 = 0
1 or 0 = 1
not 1 = 0</pre>
=={{header|V}}==
Using stack shuffles.
<
[get2 [dup] dip swap [dup] dip].
get2 and puts
Line 3,711 ⟶ 4,214:
swap not puts
pop
].</
Using view.
<
[get2 [a b : a b a b] view].
get2 and puts
Line 3,720 ⟶ 4,223:
swap not puts
pop
].</
Using internal defines
<
a b and puts
a b or puts
a not puts
].</
=={{header|Vala}}==
<
private static void print_logic (bool a, bool b) {
print ("a and b is %s\n", (a && b).to_string ());
Line 3,745 ⟶ 4,248:
return 0;
}
}</
=={{header|Verilog}}==
<
integer a, b;
Line 3,759 ⟶ 4,262:
$finish ;
end
endmodule</
=={{header|Visual Basic .NET}}==
<
Console.WriteLine("And " & a And b)
Console.WriteLine("Or " & a Or b)
Line 3,770 ⟶ 4,273:
Console.WriteLine("And, short-circuited " & a AndAlso b)
Console.WriteLine("Or, short-circuited " & a OrElse b)
End Function</
=={{header|Wren}}==
Wren has a built in Bool type which has two instances ''true'' and ''false'' which are also keywords.
The Bool class overrides
Unlike some other C fanily languages, the Bool class doesn't support the operators ''&'', ''|'', ''^'' and ''~'' which, in Wren, only apply to bitwise operations on unsigned 32-bit integers.
However, it does support the short-circuiting ''&&'' and ''||'' logical operators as well as the conditional (or ternary) operator ''?:'' all of which behave as expected.
<
System.print("a = %(a)")
System.print("b = %(b)")
Line 3,790 ⟶ 4,293:
var tests = [ [true, true], [true, false], [false, true], [false, false] ]
for (test in tests) f.call(test[0], test[1])</
{{out}}
Line 3,820 ⟶ 4,323:
=={{header|XLISP}}==
<
(print `(a and b = ,(and a b)))
(print `(a or b = ,(or a b)))
(print `(not a = ,(not a))) )</
=={{header|XPL0}}==
Line 3,834 ⟶ 4,337:
convenient to combine logical and bitwise operations.
<
func Logic(A, B);
Line 3,849 ⟶ 4,352:
Logic(1, 1); CrLf(0);
Logic(1, 2); CrLf(0);
]</
{{out}}
Line 3,861 ⟶ 4,364:
=={{header|XSLT}}==
<
<xsl:param name="a" select="true()"/>
<xsl:param name="b" select="false()"/>
Line 3,867 ⟶ 4,370:
<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>
</xsl:template></
=={{header|zkl}}==
Logical and/or do the logic and returns one of the ops.
<
fcn g(a,b){a or b}
fcn h(a){(not a)}</
<pre>
f(0,1) //-->0
|