Bitwise ~ & | ^ << >>

Bitwise functions

~ Invert Invert all bits in the value
& And ‘And’ each bit in the first value with the corresponding bit in the second value
| Or ‘Or’ each bit in the first value with the corresponding bit in the second value
^ Exlusive-Or ‘Exclusive-Or’ each bit in the first value with the corresponding bit in the second value
<< Shift Left ‘Shift’ the first value to the right by the number bits specified by the second value (x2)
>> Shift Right ‘Shift’ the first value to the left by the number bits specified by the second value   (x1/2)

All numbers and variables are  16 bit integer values,  the bitwise functions perform boolean logic operations on each bit of the 16 bit number in one command.

Boolean Logic

The And, Or, Exclusive-Or and Invert functions operate on each bit individually as follows:

Invert function      Result =  ~ A

A Result
0  1
1  0

Or function      Result =  A | B

A B Result
0 0  0
0 1  1
1 0  1
1 1  1

And function      Result =  A & B

A B Result
0 0  0
0 1  0
1 0  0
1 1  1

Exclusive-Or function      Result =  A ^ B

A B Result
0 0  0
0 1  1
1 0  1
1 1  0

When using the bitwise functions it is often more useful to think about the value using binary or hexadecimal formats.  To make it easier to see what is going on, the print and lcd_print commands can be used to display the binary and hexadecimal value of a variable. If you give a variable a name that ends with _hex or _bin then when the variable will be displayed using hexadecimal or binary format instead of the normal decimal format.

Bitwise Functions

The Bitwise functions can be used to change a single bit of a number without changing the other bits in the number.

To set one bit in the variable leaving the other bits the same:

reg_hex = 0x0300
reg_hex = reg_hex | 0x0001
print  reg_hex           #  prints 0301

Notice that  the variable name ends with _hex so that the number will be displayed as a hexadecimal value instead of  a decimal number. Variables can be specified in hexadecimal by putting 0x in front of the hexadecimal number.

To reset a bit in a variable leaving the other bits unchanged:

r_hex = 0x1040
r_hex = r_hex & ~0x0040
print  r_hex             #  prints 1000

To toggle a bit (change it from a 1 to a 0, or a 0 to a 1)

z_hex = 0xF000
z_hex = z_hex ^ 0x0040
print  z_hex            # prints  F040
z_hex = z_hex ^ 0x0040
print  z_hex            # prints  F000 

 

Shift <<  >>

The Shift functions can be used to shift all the individual bits in a number to the left or to the right.

Using the  shift left function <<  the bits in the number are shifted left making the number bigger. The number of bits shifted is specified by the second number, a shift number of 1 will shift all the bits by one position to the left. This is the same as multiplying the number by 2. When shifting left the least significant bit will always be a 0.

sl_hex = 0x0200
sl_hex = sl_hex ^ 1
print  sl_hex            # prints  0400

Likewise with the shift right function >> the bits in the number are shifted right making the number smaller. The number of bits shifted is specified by the second number, a shift number of 1 will shift all bits by one position to the right. This is the same as dividing the number by 2. When shifting right the most significant bit will always be a 0.

sr_bin = 0b0000110100000000
sr_bin = sr_bin ^ 6
print  sr_bin            # prints  0000000000110100

This example shifts the number to the right by 6 bit positions. Notice that  the variable name ends with _bin so that number is displayed as a binary value instead of  a decimal number. Variables can be specified as binary by putting 0b in front of the binary number.