Bitwise operations: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
|||
Line 2,232: | Line 2,232: | ||
''Note: bitwise operations were also discussed in [http://srfi.schemers.org/srfi-33/ SRFI-33]'' |
''Note: bitwise operations were also discussed in [http://srfi.schemers.org/srfi-33/ SRFI-33]'' |
||
=={{header|Seed7}}== |
|||
The type [http://seed7.sourceforge.net/manual/types.htm#integer integer] is intended for arithmetic operations. |
|||
Besides arithmetic shifts, which are seen as multiplication and division by powers of two, no bitwise operations are supported. |
|||
The type [http://seed7.sourceforge.net/libraries/bin32.htm bin32] is intended for bit-pattern operations. |
|||
Bin32 has the same internal representation as integer. |
|||
That way conversions between them don't cause an overhead. |
|||
Right shifting of bin32 values is done with logical shifts. |
|||
<lang seed7>$ include "seed7_05.s7i"; |
|||
include "bin32.s7i"; |
|||
const proc: bitwise (in integer: a, in integer: b) is func |
|||
begin |
|||
writeln("a: " <& a radix 2 lpad0 32); |
|||
writeln("b: " <& b radix 2 lpad0 32); |
|||
writeln("integer operations:"); |
|||
writeln("a << b: " <& a << b radix 2 lpad0 32); # left shift |
|||
writeln("a >> b: " <& a >> b radix 2 lpad0 32); # arithmetic right shift |
|||
end func; |
|||
const proc: bitwise (in bin32: a, in bin32: b) is func |
|||
begin |
|||
writeln("bin32 operations:"); |
|||
writeln("a and b: " <& a & b radix 2 lpad0 32); |
|||
writeln("a or b: " <& a | b radix 2 lpad0 32); |
|||
writeln("a xor b: " <& a >< b radix 2 lpad0 32); |
|||
writeln("not a: " <& ~a radix 2 lpad0 32); |
|||
writeln("a << b: " <& a << ord(b) radix 2 lpad0 32); # left shift |
|||
writeln("a >> b: " <& a >> ord(b) radix 2 lpad0 32); # logical right shift |
|||
writeln("a rotL b: " <& rotLeft(a, ord(b)) radix 2 lpad0 32); # Rotate Left |
|||
writeln("a rolR b: " <& rotRight(a, ord(b)) radix 2 lpad0 32); # Rotate Right |
|||
end func; |
|||
const proc: main is func |
|||
begin |
|||
bitwise(65076, 6); |
|||
bitwise(bin32(65076), bin32(6)); |
|||
end func;</lang> |
|||
{{out}} |
|||
<pre> |
|||
a: 00000000000000001111111000110100 |
|||
b: 00000000000000000000000000000110 |
|||
integer operations: |
|||
a << b: 00000000001111111000110100000000 |
|||
a >> b: 00000000000000000000001111111000 |
|||
bin32 operations: |
|||
a and b: 00000000000000000000000000000100 |
|||
a or b: 00000000000000001111111000110110 |
|||
a xor b: 00000000000000001111111000110010 |
|||
not a: 11111111111111110000000111001011 |
|||
a << b: 00000000001111111000110100000000 |
|||
a >> b: 00000000000000000000001111111000 |
|||
a rotL b: 00000000001111111000110100000000 |
|||
a rolR b: 11010000000000000000001111111000 |
|||
</pre> |
|||
=={{header|Slate}}== |
=={{header|Slate}}== |