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

0x4b

01001011

&

0x0c

00001100

Gives

0x08

00001000

Add bits

0x4b

01001011

|

0x0c

00001100

Gives

0x4f

01001111

Bitwise XOR

Exclusive OR

0x4b

01001011

^

0x0c

00001100

Gives

0x47

01000111

Shift Left

0x03

<< 2

00000011

0x0c

00001100

0x03

<< 7

00000011

0x80

10000000

  • Filled with zeroes from right

  • Bits fall off to the left

Shift Right

0x30

>> 2

00110000

0x0c

00001100

0x30

>> 5

00110000

?????001

  • Bits fall off to the right

  • unsigned: filled with zeroes from left

  • signed: machine dependent

⟶ Shift operations on signed entities is nonsense anyway

Inverting (“One’s-Complement”)

~

0x4c

01001100

0xb3

10110011