[prev] 15 [next]

Bits in Bytes in Words

Values that we normally treat as atomic can be viewed as bits, e.g.
  • char = 1 byte = 8 bits    ('a' is 01100001)
  • short = 2 bytes = 16 bits    (42 is 0000000000101010)
  • int = 4 bytes = 32 bits    (42 is 0000000000...0000101010)
  • double = 8 bytes = 64 bits
The above are common sizes and don't apply on all hardware
(e.g. sizeof(int) might be 16 or 64 bits, sizeof(double) might be 32 bits)

C provides a set of operators that act bit-by-bit on pairs of bytes.

E.g. (10101010 & 11110000) yields 10100000   (bitwise AND)

C bitwise operators:    &  |  ^  ~  <<  >>