Bit-Operators¶
Bit Manipulation¶
C is a hardware language
⟶ many operators to manipulate individual bits
|
bitwise AND |
---|---|
|
bitwise OR |
|
bitwise XOR |
|
shift left |
|
shift right |
|
bitwise invert |
Why?*
Manipulating hardware registers
Saving space (e.g. persistence, protocols)
…
Bitwise AND and OR¶
Extract/mask bits
|
|
|
|
|
|
Gives |
|
|
Add bits
|
|
|
|
|
|
Gives |
|
|
Bitwise XOR¶
Exclusive OR
|
|
|
|
|
|
Gives |
|
|
Shift Left¶
|
|
|
|
|
|
|
|
|
|
|
Filled with zeroes from right
Bits fall off to the left
Shift Right¶
|
|
|
|
|
|
|
|
|
|
Bits fall off to the right
unsigned
: filled with zeroes from leftsigned
: machine dependent
⟶ Shift operations on signed entities is nonsense anyway
Inverting (“One’s-Complement”)¶
|
|
|
|
|