Bitwise operations: Difference between revisions

Content deleted Content added
Albedo (talk | contribs)
m →‎{{header|Babel}}: Updated to interactive-mode syntax
Line 284: Line 284:
=={{header|Babel}}==
=={{header|Babel}}==


In Babel, we prefix the logic operators with a 'c' to denote that they are C-style operations, that is, they are word-width operations, not arbitrary size operations. The following program combines the numbers 5 and 9 using the various bitwise operators and then displays the results.
<lang babel>((main { (5 9) foo ! })


<lang babel>({5 9}) ({cand} {cor} {cnor} {cxor} {cxnor} {shl} {shr} {ashr} {rol}) cart ! {give <- cp -> compose !} over ! {eval} over ! {;} each</lang>
(foo {
({cand} {cor} {cnor} {cxor} {cxnor} {cushl} {cushr} {cashr} {curol} {curor})
{ <- dup give ->
eval
%x nl <<}
each
give zap
cnot %x nl <<}))</lang>


{{Out}}
This program produces the following output:
<pre>[val 0x1 ]
[val 0xd ]
[val 0xfffffff7 ]
[val 0xc ]
[val 0xfffffff3 ]
[val 0xa00 ]
[val 0x0 ]
[val 0x0 ]
[val 0xa00 ]</pre>


The cnot operator works on just one operand:
<lang babel>1

d
<lang babel>9 cnot ;</lang>
fffffff7

c
{{Out}}
fffffff3
<pre>[val 0xfffffff6 ]</pre>
a00
0
0
a00
2800000
fffffffa</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==