Rebol3 Code Examplex
Bitwise operations
Demonstrate fundamental bitwise operators and their effects.
Rebol [
title: "Rosetta code: Bitwise operations"
file: %Bitwise_operations.r3
url: https://rosettacode.org/wiki/Bitwise_operations
needs: 3.10.0
]
do-test: function [code][
res: try code
case [
error? res [res: join "error: " res/id]
integer? res [res: enbase to-binary res 2]
]
printf [-21 " == "] reduce [form code res]
]
foreach test [
[10]
[2]
[10 AND 2]
[10 & 2]
[10 OR 2]
[10 | 2]
[10 XOR 2]
[10 >> 2]
[10 << 2]
[-65432]
[complement -65432]
[255]
[shift 255 -2]
[shift 255 56]
[shift/logical 255 56]
][ do-test test ]Output:
10 == 0000000000000000000000000000000000000000000000000000000000001010
2 == 0000000000000000000000000000000000000000000000000000000000000010
10 AND 2 == 0000000000000000000000000000000000000000000000000000000000000010
10 & 2 == 0000000000000000000000000000000000000000000000000000000000000010
10 OR 2 == 0000000000000000000000000000000000000000000000000000000000001010
10 | 2 == 0000000000000000000000000000000000000000000000000000000000001010
10 XOR 2 == 0000000000000000000000000000000000000000000000000000000000001000
10 >> 2 == 0000000000000000000000000000000000000000000000000000000000000010
10 << 2 == 0000000000000000000000000000000000000000000000000000000000101000
-65432 == 1111111111111111111111111111111111111111111111110000000001101000
complement -65432 == 0000000000000000000000000000000000000000000000001111111110010111
255 == 0000000000000000000000000000000000000000000000000000000011111111
shift 255 -2 == 0000000000000000000000000000000000000000000000000000000000111111
shift 255 56 == error: overflow
shift/logical 255 56 == 1111111100000000000000000000000000000000000000000000000000000000