
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate.

All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer.

If any operation is not available in your language, note it.




window 1, @"Bitwise Operations", (0,0,650,270)
 
def fn rotl( b as long, n as long ) as long = ( ( 2^n * b) mod 256) or (b > 127)
def fn rotr( b as long, n as long ) as long = (b >> n mod 32) or ( b << (32-n) mod 32)
 
local fn bitwise( a as long, b as long )
  print @"Input: a = "; a; @"  b = "; b
  print
  print @"AND  :", @"a && b = ", bin(a && b), @": "; a && b
  print @"NAND :", @"a ^& b = ", bin(a ^& b), @": "; a ^& b
  print @"OR   :", @"a || b = ", bin(a || b), @": "; a || b
  print @"NOR  :", @"a ^| b = ", bin(a ^| b), @": "; a ^| b
  print @"XOR  :", @"a ^^ b = ", bin(a ^^ b), @": "; a ^^ b
  print @"NOT  :", @" not a = ", bin( not a), @": ";  not a
  print
  print @"Left shift   :", @"a << b =", bin(a << b), @": "; a << b
  print @"Right shift  :", @"a >> b =", bin(a >> b), @": "; a >> b
  print
  print @"Rotate left  :", @"fn rotl( a, b ) = ", bin(fn rotl( a, b)), @": "; fn rotl( a, b )
  print @"Rotate right :", @"fn rotr( a, b ) = ", bin(fn rotr( a, b )),@": "; fn rotr( a, b )
end fn
 
fn bitwise( 255, 2 )
 
HandleEvents