Logical operations: Difference between revisions

 
(15 intermediate revisions by 11 users not shown)
Line 203:
=={{header|Agda}}==
 
===Short version===
<syntaxhighlight lang="agda">module AndOrNot where
<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 xa yb = xayb , xayb , not x</syntaxhighlight>a
</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}}==
Line 600 ⟶ 652:
 
=={{header|BASIC}}==
 
==={{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|BASIC256}}===
<syntaxhighlight lang="basic256">a = true
Line 653 ⟶ 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}}===
Line 664 ⟶ 742:
170 PRINT 2 BOR 15
180 PRINT (A BOR B)-(A BAND B) ! xor</syntaxhighlight>
 
==={{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}}===
Line 688 ⟶ 777:
PRINT NOT a
END SUB</syntaxhighlight>
 
==={{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 816 ⟶ 911:
print "not a: ", !a, "\n"
}</syntaxhighlight>
 
=={{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}}==
Line 1,181 ⟶ 1,279:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">func logic a b . .
proc iflogic a = 1 and b =. 1.
if r1a = 1 and b = 1
r1 = 1
.
.
if a = 1 or b = 1
if r2a = 1 or b = 1
r2 = 1
.
if a = 0.
if r3a = 10
r3 = 1
.
.
print r1 & " " & r2 & " " & r3
print r1 & " " & r2 & " " & r3
.
call logic 0 0
call logic 0 1
call logic 1 0
call logic 1 1</syntaxhighlight>
</syntaxhighlight>
 
=={{header|ECL}}==
Line 1,307 ⟶ 1,407:
--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}}==
Line 1,842 ⟶ 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}}==
Line 1,881 ⟶ 2,043:
│└─────┴───────┘│└─────┴───────┘│└─────┴───────┘│
└───────────────┴───────────────┴───────────────┘</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}}==
Line 2,063 ⟶ 2,244:
 
=={{header|langur}}==
The logical operators in langur compare the "truthiness" of the left and right operands and do not require Booleans. A null is a non-truthy result.
 
The operators and, or, nand, nor, and?, or?, nand?, nor?, xor?, and nxor? are short-circuiting.
Line 2,069 ⟶ 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="langur">val .test = ffn(.a, .b) join("\n",{ [
join("\n", [
$"not \.a;: \{not .a}",
$"\not {{.a;}}: and{{not \.b;: \.a and .b;}}",
$"\{{.a;}} orand \{{.b;}}: \{{.a orand .b;}}",
$"\{{.a;}} nand \{{.b;}}: \{{.a nand .b;}}",
$"\{{.a;}} noror \{{.b;}}: \{{.a noror .b;}}",
$"\{{.a;}} xornor \{{.b;}}: \{{.a xornor .b;}}",
$"\{{.a;}} nxorxor \{{.b;}}: \{{.a nxorxor .b;}}",
"{{.a}} nxor {{.b}}: {{.a nxor .b}}",
"",
"",
 
$"not? \.a;: \{not? .a}",
$"\.a; andnot? \{{.b;a}}: \.a and{{not? .b;a}}",
$"\{{.a;}} orand? \{{.b;}}: \{{.a orand? .b;}}",
$"\{{.a;}} nand? \{{.b;}}: \{{.a nand? .b;}}",
$"\{{.a;}} noror? \{{.b;}}: \{{.a noror? .b;}}",
$"\{{.a;}} xornor? \{{.b;}}: \{{.a xornor? .b;}}",
$"\{{.a;}} nxorxor? \{{.b;}}: \{{.a nxorxor? .b;}}",
"{{.a}} nxor? {{.b}}: {{.a nxor? .b}}",
"\n",
"\n",
])
])
}
 
val .tests = [
Line 2,812 ⟶ 2,995:
echo "not a: ", not a
echo "a xor b: ", a xor b</syntaxhighlight>
 
=={{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}}==
Line 3,662 ⟶ 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}}==
<syntaxhighlight lang="ruby">def logic(a, b)
Line 4,070 ⟶ 4,278:
Wren has a built in Bool type which has two instances ''true'' and ''false'' which are also keywords.
 
The Bool class overrides, the ''!'' operator which it inherits from the Object class so that ''!true'' is false and ''!false'' is true as one would expect.
 
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.
<syntaxhighlight lang="ecmascriptwren">var f = Fn.new { |a, b|
System.print("a = %(a)")
System.print("b = %(b)")
885

edits