47 lines
1.3 KiB
Plaintext
47 lines
1.3 KiB
Plaintext
' 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
|