Bitwise (Boolean) logic operator

When you use byte or word sized data as inputs for Bitwise operators, the result can be as long as your longest input.  The operator will perform a comparison of each bit in both input variables, and store the result in the corresponding bit location of the result variable.

A common example is the AND operator used to mask certain bits in a byte. It works as a mask because it always returns 0 when one of the inputs is 0.  Therefore, when we use "bytevar & %00001111", the top 4 bits of the result will always be 0.  The lower 4 bits won't change, because ANDing something with a logic 1 makes the result equal to the input.

Here's a modified truth table showing 8-bits being ANDed all at once.

bit position byte A byte B A & B
7 1 0 0
6 0 0 0
5 1 0 0
4 1 0 0
3 0 1 0
2 1 1 1
1 1 1 1
0 0 1 0
PICBASIC example:

A = %10110110
B = %00001111
result = A & B

'(00000110)

Here are some example equations using the input values above:

%10110110 | %00001111 = %10111111     'use OR to mask bits with logic 1

%10110110 ^ %00001111 = %10111001     'use XOR to invert selected bits