
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.




 
'   bitwise operations on byte-sized variables
 
v =int( 256 *rnd( 1))
 
s = 1
 
print "Shift            ="; s; " place."
print
print "Number as dec. = "; v; " & as 8-bits byte = ", dec2Bin$( v)
print "NOT  as dec.          =  "; bitInvert(   v),    dec2Bin$( bitInvert(   v))
print "Shifted left  as dec. =  "; shiftLeft(   v, s), dec2Bin$( shiftLeft(   v, s))
print "Shifted right as dec. =  "; shiftRight(  v, s), dec2Bin$( shiftRight(  v, s))
print "Rotated left  as dec. =  "; rotateLeft(  v, s), dec2Bin$( rotateLeft(  v, s))
print "Rotated right as dec. =  "; rotateRight( v, s), dec2Bin$( rotateRight( v, s))
 
end
 
function shiftLeft( b, n)
    shiftLeft =( b *2^n) and 255
end function
 
function shiftRight( b, n)
    shiftRight =int(b /2^n)
end function
 
function rotateLeft( b, n)
    rotateLeft = (( 2^n *b) mod 256) or ( b >127)
end function
 
function rotateRight( b, n)
    rotateRight = (128*( b and 1)) or int( b /2)
end function
 
function bitInvert( b)
    bitInvert =b xor 255
end function
 
function dec2Bin$( num) '   Given an integer decimal 0 <--> 255, returns binary equivalent as a string
    n =num
    dec2Bin$ =""
    while ( num >0)
        dec2Bin$    =str$( num mod 2) +dec2Bin$
        num         =int(  num /2)
    wend
    dec2Bin$ =right$( "00000000" +dec2Bin$, 8)
end function
 