<< Click to Display Table of Contents >> Bitwise Operators |
|
The last group of operators are called bitwise operators because the operations are performed bitwise. It simply means that a logic operation is performed for each bit of two numbers. The result is a new number – the total result of the bitwise operations.
▪&
▪|
▪^
▪<<
▪>>
Example:
15 & 8
Result:
8
Since this operation is bitwise the calculation will be per bit. So to understand what’s going on here, you have to convert the numbers to binary values:
15 = 1111 8 = 1000
Now each bit in the number 1111 (15) can be used in a logical operation with the other number 1000 (8): 1111 AND 1000
Bit number |
1111 (15) |
1000 (8) |
Result |
---|---|---|---|
0 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
2 |
1 |
0 |
0 |
3 |
1 |
1 |
1 |