Boolean (bitwise) operators
Jump to navigation
Jump to search
Description
- Actually, JB logical operators are bit-wise operators:
- AND, OR and XOR
- It looks like they operate on 32-bit integers.
- There is no bit-wise NOT, but you can compute it as
- x XOR -1
- (-1 has all 32 bits set to 1, see results of BitPattern$ under Useful Procedures)
- There is no bit-wise NOT, but you can compute it as
Syntax
- Place all possible forms of the keyword here
Hints
- Using logical operators on floating-point numbers makes no sense.
- Sometimes, it will fail with a "IsEmpty not understood" error.
- (Do NOT expect JB to convert numbers to integers for you in this case.)
- Using logical operators on big negative numbers sometimes will fail with a "IsEmpty not understood" error too.
- It looks like operator precedence not implemented - use parentheses instead.
Example
print "Example of bitwise operations usage" print "we will work with 8 bit, for simplicity." print "You can sure use up to 32 bit in JB." print print "Let's start from 0" a=0 print tab(5);eightBitPattern$(a) print print "To set some bits, we use OR" op = a op1$ = "1010" op1 = bin2num(op1$) print tab(5);eightBitPattern$(op) print " OR ";eightBitPattern$(op1) print tab(5);"--------" res = op or op1 print tab(5);eightBitPattern$(res) print print "Now, add some more bits" op = res op1$ = "11" op1 = bin2num(op1$) print tab(5);eightBitPattern$(op) print " OR ";eightBitPattern$(op1) print tab(5);"--------" res = op or op1 print tab(5);eightBitPattern$(res) print "note last bit got set, but one just before doesn't change (stays set)" print print "To mask (reset) some bits, we use AND" print "second operator commonly called a mask" print "bits where mask has 0 will get reset (masked)" op = res mask$="11111001" op1 = bin2num(mask$) print tab(5);eightBitPattern$(op) print "AND ";eightBitPattern$(op1); "<< the mask" print tab(5);"--------" res = op and op1 print tab(5);eightBitPattern$(res) print print "To toggle some bits, we use XOR" print "let's toggle first and last bit" op = res toggeMask$="10000001" op1 = bin2num(toggeMask$) print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"--------" res = op xor op1 print tab(5);eightBitPattern$(res) print print "Second XOR with same number restores original" op = res print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"--------" res = op xor op1 print tab(5);eightBitPattern$(res) print print "Last, XOR with all the ones works as bitwise NOT" op = res toggeMask$="11111111" op1 = bin2num(toggeMask$) print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"--------" res = op xor op1 print tab(5);eightBitPattern$(res) print end '--------------------------------------------- function bin2num(bin$) bin2num = 0 for i = 1 to len(bin$) c$=mid$(bin$, i, 1) if instr("01",c$)=0 then exit function 'like VAL(), break on first non-valid digit bin2num = bin2num*2+(instr("01",c$)-1) next end function function eightBitPattern$(num) for i = 0 to 7 eightBitPattern$ = str$((num and 2 ^ i) > 0) + eightBitPattern$ next i end function
Useful Procedures
function BitPattern$(num) for i = 0 to 31 if i mod 8 = 0 then BitPattern$ = " " + BitPattern$ BitPattern$ = str$((num and 2 ^ i) > 0) + BitPattern$ next i end function
function bitwiseNot(num) bitwiseNot = num XOR -1 end function