Bitwise operations: Difference between revisions

m
imported>KayproKid
(Added S-BASIC example)
 
(7 intermediate revisions by 3 users not shown)
Line 3,902:
alert("a >>> b: " + (a >>> b)); // logical right shift
}</syntaxhighlight>
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
jq has no built-in bitwise operations, but the
[[:Category:Jq/bitwise.jq | "bitwise" module]]
defines
all those needed for the task at hand except for rotations.
Here `rotateLeft` and rotateRight` functions are defined relative to a given width.
 
The examples are taken from the entry for [[#Wren|Wren]].
<syntaxhighlight lang="jq">
include "bitwise" {search: "."}; # adjust as required
 
def leftshift($n; $width):
[(range(0,$n)| 0), limit($width - $n; bitwise)][:$width] | to_int;
 
# Using a width of $width bits: x << n | x >> ($width-n)
def rotateLeft($x; $n; $width):
$x | bitwise_or(leftshift($n; $width); rightshift($width-$n));
 
# Using a width of $width bits: x << n | x >> ($width-n)
def rotateRight($x; $n; $width):
$x | bitwise_or(rightshift($n); leftshift($width-$n; $width) );
 
def task($x; $y):
def isInteger: type == "number" and . == round;
if ($x|isInteger|not) or ($y|isInteger|not) or
$x < 0 or $y < 0 or $x > 4294967295 or $y > 4294967295
then "Operands must be in the range of a 32-bit unsigned integer" | error
else
" x = \($x)",
" y = \($y)",
" x & y = \(bitwise_and($x; $y))",
" x | y = \(bitwise_or($x; $y))",
" x ^ y = \(null | xor(x; $y))",
"~x = \(32 | flip($x))",
" x << y = \($x | leftshift($y))",
" x >> y = \($x | rightshift($y))",
" x rl y = \(rotateLeft($x; $y; 32))",
" x rr y = \(rotateRight($x; $y; 32))"
end;
 
task(10; 2)
</syntaxhighlight>
{{output}}
<pre>
x = 10
y = 2
x & y = 2
x | y = 10
x ^ y = 8
~x = 4294967295
x << y = 40
x >> y = 2
x rl y = 40
x rr y = 2147483650
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia"># Version 5.2
Line 3,934 ⟶ 3,994:
rol(A,5) = Bool[true,true,false,false,true]
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
Line 5,661 ⟶ 5,722:
=={{header|S-BASIC}}==
S-BASIC does not have bitwise shift or rotate operators. The test values are taken from the 11l example.
<syntaxhighlight lang="BASIC">
var a, b = integer
a = 10
Line 5,697 ⟶ 5,758:
println("a rol b: " + Integer.rotateRight(a, b)) // Rotate Right
}</syntaxhighlight>
 
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^6</math>RS}}
Line 6,156 ⟶ 6,218:
setMode("Base",oldbase)
EndPrgm</syntaxhighlight>
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">|00 @System [ &vector $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1 ]
|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 &error $1 ]
 
( program )
|0100 @on-reset ( -> )
#0a02
DUP2 SWP ;Labels/a <print-arg> ;Labels/b <print-arg>
bitwise
halt
BRK
 
@bitwise ( a b -- )
;Labels/not <print-str> ;Labels/a <print-str> ;Labels/equ <print-str> DUP2 [ POP #ff EOR ] <print-result>
;Labels/and <print-label> DUP2 [ AND ] <print-result>
;Labels/or <print-label> DUP2 [ ORA ] <print-result>
;Labels/xor <print-label> DUP2 [ EOR ] <print-result>
;Labels/shl <print-label> DUP2 [ #40 SFT SFT ] <print-result>
;Labels/shr <print-label> DUP2 [ SFT ] <print-result>
;Labels/rol <print-label> DUP2 [ #40 SFT #00 ROT ROT SFT2 ORA ] <print-result>
;Labels/ror <print-label> [ SWP #00 ROT SFT2 ORA ] <print-result>
JMP2r
 
@halt ( -- )
#01 .System/halt DEO
BRK
 
@<print-arg> ( a name* -- )
<print-str> ;Labels/equ <print-str> <print-result>
JMP2r
 
@<print-result> ( a -- )
<print-hex> ;Labels/newline <print-str>
JMP2r
 
@<print-label> ( label* -- )
;Labels/a <print-str>
<print-str>
;Labels/b <print-str>
;Labels/equ <print-str>
JMP2r
 
@<print-hex> ( byte -- )
[ LIT "$ ] .Console/write DEO
DUP #04 SFT <print-hex>/l
&l ( -- )
#0f AND DUP #09 GTH #27 MUL ADD [ LIT "0 ] ADD .Console/write DEO
JMP2r
 
@<print-str> ( str* -- )
&while ( -- )
LDAk .Console/write DEO
INC2 LDAk ?&while
POP2 JMP2r
 
@Labels
&a "a 20 $1
&b "b 20 $1
&equ "= 20 $1
&newline 0a $1
&not "NOT 20 $1
&and "AND 20 $1
&or "OR 20 $1
&xor "XOR 20 $1
&shl "SHL 20 $1
&shr "SHR 20 $1
&rol "ROL 20 $1
&ror "ROR 20 $1
</syntaxhighlight>
{{out}}
<pre>a = $0a
b = $02
NOT a = $f5
a AND b = $02
a OR b = $0a
a XOR b = $08
a SHL b = $28
a SHR b = $02
a ROL b = $28
a ROR b = $82
</pre>
 
=={{header|Vala}}==
<syntaxhighlight lang="vala">void testbit(int a, int b) {
2,442

edits