Bitwise operations: Difference between revisions
Content added Content deleted
m (→{{header|PL/I}}: missing comment delimiters) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 2,237: | Line 2,237: | ||
rorl %cl, %eax ; right rotate x by s |
rorl %cl, %eax ; right rotate x by s |
||
ret</lang> |
ret</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>static void bitwise(int a, int b) |
|||
{ |
|||
Console.WriteLine("a and b is {0}", a & b); |
|||
Console.WriteLine("a or b is {0}", a | b); |
|||
Console.WriteLine("a xor b is {0}", a ^ b); |
|||
Console.WriteLine("not a is {0}", ~a); |
|||
Console.WriteLine("a lshift b is {0}", a << b); |
|||
Console.WriteLine("a arshift b is {0}", a >> b); // When the left operand of the >> operator is of a signed integral type, |
|||
// the operator performs an arithmetic shift right |
|||
uint c = (uint)a; |
|||
Console.WriteLine("c rshift b is {0}", c >> b); // When the left operand of the >> operator is of an unsigned integral type, |
|||
// the operator performs a logical shift right |
|||
// there are no rotation operators in C# |
|||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 2,254: | Line 2,270: | ||
// there are no rotation operators in C++ |
// there are no rotation operators in C++ |
||
}</lang> |
}</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>static void bitwise(int a, int b) |
|||
{ |
|||
Console.WriteLine("a and b is {0}", a & b); |
|||
Console.WriteLine("a or b is {0}", a | b); |
|||
Console.WriteLine("a xor b is {0}", a ^ b); |
|||
Console.WriteLine("not a is {0}", ~a); |
|||
Console.WriteLine("a lshift b is {0}", a << b); |
|||
Console.WriteLine("a arshift b is {0}", a >> b); // When the left operand of the >> operator is of a signed integral type, |
|||
// the operator performs an arithmetic shift right |
|||
uint c = (uint)a; |
|||
Console.WriteLine("c rshift b is {0}", c >> b); // When the left operand of the >> operator is of an unsigned integral type, |
|||
// the operator performs a logical shift right |
|||
// there are no rotation operators in C# |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 2,641: | Line 2,642: | ||
255 bsr 170 = 0 |
255 bsr 170 = 0 |
||
</lang> |
</lang> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 2,769: | Line 2,769: | ||
ROT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11000000000000000000000000000001 -1073741823 |
ROT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11000000000000000000000000000001 -1073741823 |
||
</lang> |
</lang> |
||
=={{header|Free Pascal}}== |
|||
<lang pascal>program Bitwise; |
|||
{$mode objfpc} |
|||
var |
|||
// Pascal uses a native int type as a default literal type |
|||
// Make sure the operants work on an exact type. |
|||
x:shortint = 2; |
|||
y:ShortInt = 3; |
|||
begin |
|||
Writeln('2 and 3 = ', x and y); |
|||
Writeln('2 or 3 = ', x or y); |
|||
Writeln('2 xor 3 = ', x xor y); |
|||
Writeln('not 2 = ', not x); |
|||
Writeln('2 shl 3 = ', x >> y); |
|||
Writeln('2 shr 3 = ', x << y); |
|||
writeln('2 rol 3 = ', rolbyte(x,y)); |
|||
writeln('2 ror 3 = ', rorbyte(x,y)); |
|||
writeln('2 sar 3 = ', sarshortint(x,y)); |
|||
Readln; |
|||
end.</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Line 2,857: | Line 2,878: | ||
x ROR y = 4611686018427387902 |
x ROR y = 4611686018427387902 |
||
</pre> |
</pre> |
||
=={{header|Free Pascal}}== |
|||
<lang pascal>program Bitwise; |
|||
{$mode objfpc} |
|||
var |
|||
// Pascal uses a native int type as a default literal type |
|||
// Make sure the operants work on an exact type. |
|||
x:shortint = 2; |
|||
y:ShortInt = 3; |
|||
begin |
|||
Writeln('2 and 3 = ', x and y); |
|||
Writeln('2 or 3 = ', x or y); |
|||
Writeln('2 xor 3 = ', x xor y); |
|||
Writeln('not 2 = ', not x); |
|||
Writeln('2 shl 3 = ', x >> y); |
|||
Writeln('2 shr 3 = ', x << y); |
|||
writeln('2 rol 3 = ', rolbyte(x,y)); |
|||
writeln('2 ror 3 = ', rorbyte(x,y)); |
|||
writeln('2 sar 3 = ', sarshortint(x,y)); |
|||
Readln; |
|||
end.</lang> |
|||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
Line 3,202: | Line 3,202: | ||
]; |
]; |
||
</lang> |
</lang> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 3,548: | Line 3,549: | ||
a lshift -b: 7 |
a lshift -b: 7 |
||
-a ashift -b: -8</lang> |
-a ashift -b: -8</lang> |
||
=={{header|LSE64}}== |
|||
{{incorrect|LSE64|No reason given.}} |
|||
<lang lse64>over : 2 pick |
|||
2dup : over over |
|||
bitwise : \ |
|||
" A=" ,t over ,h sp " B=" ,t dup ,h nl \ |
|||
" A and B=" ,t 2dup & ,h nl \ |
|||
" A or B=" ,t 2dup | ,h nl \ |
|||
" A xor B=" ,t over ^ ,h nl \ |
|||
" not A=" ,t ~ ,h nl |
|||
\ a \ 7 bitwise # hex literals</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 3,624: | Line 3,639: | ||
& (and), | (or), ^^ (xor), << (logical shift left), >> (logical shift right) |
& (and), | (or), ^^ (xor), << (logical shift left), >> (logical shift right) |
||
and a unary operation ~ (negate). |
and a unary operation ~ (negate). |
||
=={{header|LSE64}}== |
|||
{{incorrect|LSE64|No reason given.}} |
|||
<lang lse64>over : 2 pick |
|||
2dup : over over |
|||
bitwise : \ |
|||
" A=" ,t over ,h sp " B=" ,t dup ,h nl \ |
|||
" A and B=" ,t 2dup & ,h nl \ |
|||
" A or B=" ,t 2dup | ,h nl \ |
|||
" A xor B=" ,t over ^ ,h nl \ |
|||
" not A=" ,t ~ ,h nl |
|||
\ a \ 7 bitwise # hex literals</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Line 4,119: | Line 4,120: | ||
print 'a >> b: ', $a >> $b, "\n"; # arithmetic right shift |
print 'a >> b: ', $a >> $b, "\n"; # arithmetic right shift |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2017.05}} |
|||
<lang perl6>constant MAXINT = uint.Range.max; |
|||
constant BITS = MAXINT.base(2).chars; |
|||
# define rotate ops for the fun of it |
|||
multi sub infix:<⥁>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).list.rotate(b).reverse] } |
|||
multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.list.rotate(b)] } |
|||
sub int-bits (Int $a, Int $b) { |
|||
say ''; |
|||
say_bit "$a", $a; |
|||
say ''; |
|||
say_bit "2's complement $a", +^$a; |
|||
say_bit "$a and $b", $a +& $b; |
|||
say_bit "$a or $b", $a +| $b; |
|||
say_bit "$a xor $b", $a +^ $b; |
|||
say_bit "$a unsigned shift right $b", ($a +& MAXINT) +> $b; |
|||
say_bit "$a signed shift right $b", $a +> $b; |
|||
say_bit "$a rotate right $b", $a ⥁ $b; |
|||
say_bit "$a shift left $b", $a +< $b; |
|||
say_bit "$a rotate left $b", $a ⥀ $b; |
|||
} |
|||
int-bits(7,2); |
|||
int-bits(-65432,31); |
|||
sub say_bit ($message, $value) { |
|||
printf("%30s: %{'0' ~ BITS}b\n", $message, $value +& MAXINT); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> 7: 0000000000000000000000000000000000000000000000000000000000000111 |
|||
2's complement 7: 1111111111111111111111111111111111111111111111111111111111111000 |
|||
7 and 2: 0000000000000000000000000000000000000000000000000000000000000010 |
|||
7 or 2: 0000000000000000000000000000000000000000000000000000000000000111 |
|||
7 xor 2: 0000000000000000000000000000000000000000000000000000000000000101 |
|||
7 unsigned shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 |
|||
7 signed shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 |
|||
7 rotate right 2: 1100000000000000000000000000000000000000000000000000000000000001 |
|||
7 shift left 2: 0000000000000000000000000000000000000000000000000000000000011100 |
|||
7 rotate left 2: 0000000000000000000000000000000000000000000000000000000000011100 |
|||
-65432: 1111111111111111111111111111111111111111111111110000000001101000 |
|||
2's complement -65432: 0000000000000000000000000000000000000000000000001111111110010111 |
|||
-65432 and 31: 0000000000000000000000000000000000000000000000000000000000001000 |
|||
-65432 or 31: 1111111111111111111111111111111111111111111111110000000001111111 |
|||
-65432 xor 31: 1111111111111111111111111111111111111111111111110000000001110111 |
|||
-65432 unsigned shift right 31: 0000000000000000000000000000000111111111111111111111111111111111 |
|||
-65432 signed shift right 31: 1111111111111111111111111111111111111111111111111111111111111111 |
|||
-65432 rotate right 31: 1111111111111110000000001101000111111111111111111111111111111111 |
|||
-65432 shift left 31: 1111111111111111100000000011010000000000000000000000000000000000 |
|||
-65432 rotate left 31: 1111111111111111100000000011010001111111111111111111111111111111</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 4,437: | Line 4,383: | ||
Similarly, on infinitely precise numbers rotation is undefined. |
Similarly, on infinitely precise numbers rotation is undefined. |
||
=={{header|PowerShell}}== |
|||
Logical right shift and rotations are not supported in PowerShell. |
|||
{{works with|PowerShell|2.0}} |
|||
<lang PowerShell>$X -band $Y |
|||
$X -bor $Y |
|||
$X -bxor $Y |
|||
-bnot $X</lang> |
|||
{{works with|PowerShell|3.0}} |
|||
<lang PowerShell>$X -shl $Y |
|||
# Arithmetic right shift |
|||
$X -shr $Y |
|||
# Requires quite a stretch of the imagination to call this "native" support of right rotate, but it works |
|||
[System.Security.Cryptography.SHA256Managed].GetMethod('RotateRight', 'NonPublic, Static', $null, @([UInt32], [Int32]), $null).Invoke($null, @([uint32]$X, $Y))</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Line 4,468: | Line 4,430: | ||
Debug Temp |
Debug Temp |
||
EndProcedure</lang> |
EndProcedure</lang> |
||
=={{header|PowerShell}}== |
|||
Logical right shift and rotations are not supported in PowerShell. |
|||
{{works with|PowerShell|2.0}} |
|||
<lang PowerShell>$X -band $Y |
|||
$X -bor $Y |
|||
$X -bxor $Y |
|||
-bnot $X</lang> |
|||
{{works with|PowerShell|3.0}} |
|||
<lang PowerShell>$X -shl $Y |
|||
# Arithmetic right shift |
|||
$X -shr $Y |
|||
# Requires quite a stretch of the imagination to call this "native" support of right rotate, but it works |
|||
[System.Security.Cryptography.SHA256Managed].GetMethod('RotateRight', 'NonPublic, Static', $null, @([UInt32], [Int32]), $null).Invoke($null, @([uint32]$X, $Y))</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 4,618: | Line 4,564: | ||
'(5 255 250 -256 8160 7) |
'(5 255 250 -256 8160 7) |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2017.05}} |
|||
<lang perl6>constant MAXINT = uint.Range.max; |
|||
constant BITS = MAXINT.base(2).chars; |
|||
# define rotate ops for the fun of it |
|||
multi sub infix:<⥁>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).list.rotate(b).reverse] } |
|||
multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.list.rotate(b)] } |
|||
sub int-bits (Int $a, Int $b) { |
|||
say ''; |
|||
say_bit "$a", $a; |
|||
say ''; |
|||
say_bit "2's complement $a", +^$a; |
|||
say_bit "$a and $b", $a +& $b; |
|||
say_bit "$a or $b", $a +| $b; |
|||
say_bit "$a xor $b", $a +^ $b; |
|||
say_bit "$a unsigned shift right $b", ($a +& MAXINT) +> $b; |
|||
say_bit "$a signed shift right $b", $a +> $b; |
|||
say_bit "$a rotate right $b", $a ⥁ $b; |
|||
say_bit "$a shift left $b", $a +< $b; |
|||
say_bit "$a rotate left $b", $a ⥀ $b; |
|||
} |
|||
int-bits(7,2); |
|||
int-bits(-65432,31); |
|||
sub say_bit ($message, $value) { |
|||
printf("%30s: %{'0' ~ BITS}b\n", $message, $value +& MAXINT); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> 7: 0000000000000000000000000000000000000000000000000000000000000111 |
|||
2's complement 7: 1111111111111111111111111111111111111111111111111111111111111000 |
|||
7 and 2: 0000000000000000000000000000000000000000000000000000000000000010 |
|||
7 or 2: 0000000000000000000000000000000000000000000000000000000000000111 |
|||
7 xor 2: 0000000000000000000000000000000000000000000000000000000000000101 |
|||
7 unsigned shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 |
|||
7 signed shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 |
|||
7 rotate right 2: 1100000000000000000000000000000000000000000000000000000000000001 |
|||
7 shift left 2: 0000000000000000000000000000000000000000000000000000000000011100 |
|||
7 rotate left 2: 0000000000000000000000000000000000000000000000000000000000011100 |
|||
-65432: 1111111111111111111111111111111111111111111111110000000001101000 |
|||
2's complement -65432: 0000000000000000000000000000000000000000000000001111111110010111 |
|||
-65432 and 31: 0000000000000000000000000000000000000000000000000000000000001000 |
|||
-65432 or 31: 1111111111111111111111111111111111111111111111110000000001111111 |
|||
-65432 xor 31: 1111111111111111111111111111111111111111111111110000000001110111 |
|||
-65432 unsigned shift right 31: 0000000000000000000000000000000111111111111111111111111111111111 |
|||
-65432 signed shift right 31: 1111111111111111111111111111111111111111111111111111111111111111 |
|||
-65432 rotate right 31: 1111111111111110000000001101000111111111111111111111111111111111 |
|||
-65432 shift left 31: 1111111111111111100000000011010000000000000000000000000000000000 |
|||
-65432 rotate left 31: 1111111111111111100000000011010001111111111111111111111111111111</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
Line 4,951: | Line 4,953: | ||
a >> b : 1 |
a >> b : 1 |
||
</pre> |
</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
<lang simula>BEGIN |
<lang simula>BEGIN |