Bitwise Operations
- Overview
- Negative number conversion
- Bitwise operations
Binary operation: manipulation of individual bits in binary data or numbers.
- Convert number into 2's complement (binary):
7 -> 0111
- Invert all the bits:
0111 -> 1000
- Add 1:
1000 -> 1001
Operator | Description | Example |
---|---|---|
AND (& ) | If both bits are 1 , the result is 1 ; otherwise 0 |
|
OR (| ) | If any bit is 1 , the result is 1 ; otherwise 0 |
|
XOR: exclusive OR (^ ) | If the bits are different, the result is 1 ; otherwise 0 |
|
NOT (~ ) | Flips the bits. Each 0 becomes 1 , and each 1 becomes 0 |
|
Signed Left Shift (<< ) | Shifts the bits of the left operand to the left by a number of positions specified by the right operand |
|
Signed Right Shift (>> ) | Shifts the bits of the left operand to the right by a number of positions specified by the right operand |
|
Unsigned Right Shift (>>> ) | Similar to the right shift operator, but it treats the value as an unsigned integer. It fills the vacant bits on the left with zeros |
|