Brainlag
  1. C
  2. Bits and a tiny machine
35/38 lessons

Shifts and masks

40 min4 exercisesPremium

Move bits with << and >>, build masks, and pack and unpack fields such as the red, green and blue parts of a colour.

The skill
Move bits into place with shifts and pull fields out of a packed number with masks.
You make
A colour splitter for web colours, a packet header reader and a save game packed into one number.

Premium lesson

£7.99 a month or £59 a year. Cancel any time.

Builds on 34 Bitwise operators. Free to try: C Foundations and the playground.

Step 1 of 7
exercise_1.c

You've reached the good stuff.

4 exercises in this lesson, checked as you type. Premium opens every lesson after Foundations, in all four courses.

C, 32-bit: int and pointers are 4 bytes