Bitwise Operators
Use bitwise operators to modify individual bits of numerical operands.
Bitwise operators associate from left to right. The only exception is the bitwise complement operator not
which associates from right to left.
Bitwise Operators Overview
Operator | Operation |
---|---|
and | bitwise AND; compares pairs of bits and returns 1 if both bits are 1, otherwise it returns 0 |
or | bitwise (inclusive) OR; compares pairs of bits and generates a 1 result if either or both bits are 1, otherwise it returns 0 |
xor | bitwise exclusive OR (XOR); compares pairs of bits and generates a 1 result if the bits are complementary, otherwise it returns 0 |
not | bitwise complement (unary); inverts each bit |
<< | bitwise shift left; moves the bits to the left, discards the far left bit and assigns 0 to the right most bit. |
>> | bitwise shift right; moves the bits to the right, discards the far right bit and if unsigned assigns 0 to the left most bit, otherwise sign extends |
Logical Operations on Bit Level
and | 0 | 1 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
or | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 1 |
xor | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 0 |
not | 0 | 1 |
---|---|---|
1 | 0 |
The bitwise operators and
, or
, and xor
perform logical operations on the appropriate pairs of bits of their operands. The operator not
complements each bit of its operand. For example:
$1234 and $5678 ' equals $1230 ' because .. ' $1234 : 0001 0010 0011 0100 ' $5678 : 0101 0110 0111 1000 ' ---------------------------- ' and : 0001 0010 0011 0000 ' .. that is, $1230
' Similarly: $1234 or $5678 ' equals $567C $1234 xor $5678 ' equals $444C not $1234 ' equals $EDCB
Unsigned and Conversions
If a number is converted from less complex to more complex data type, the upper bytes are filled with zeroes. If a number is converted from more complex to less complex data type, the data is simply truncated (the upper bytes are lost).
For example:
dim a as byte dim b as word ' ... a = $AA b = $F0F0 b = b and a ' a is extended with zeroes; b becomes $00A0
Signed and Conversions
If number is converted from less complex to more complex data type, the upper bytes are filled with ones if sign bit is 1 (number is negative); the upper bytes are filled with zeroes if sign bit is 0 (number is positive). If number is converted from more complex to less complex data type, the data is simply truncated (the upper bytes are lost).
For example:
dim a as byte dim b as word ' ... a = -12 b = $70FF b = b and a ' a is sign extended, upper byte is $FF; ' b becomes $70F4
Bitwise Shift Operators
The binary operators <<
and >>
move the bits of the left operand by a number of positions specified by the right operand, to the left or right, respectively. Right operand has to be positive and less than 255.
With shift left (<<
), left most bits are discarded, and “new” bits on the right are assigned zeroes. Thus, shifting unsigned operand to the left by n positions is equivalent to multiplying it by 2n if all discarded bits are zero. This is also true for signed operands if all discarded bits are equal to the sign bit.
With shift right (>>
), right most bits are discarded, and the “freed” bits on the left are assigned zeroes (in case of unsigned operand) or the value of the sign bit (in case of signed operand). Shifting operand to the right by n positions is equivalent to dividing it by 2n.
What do you think about this topic ? Send us feedback!