You are looking at lesson 8. Your place is still lesson 4, task 1: Style.
Binary notation, unsigned, the operators & | ^ ~ << >> and masks to set, clear and test one bit.
A byte is 8 bits, each bit 0 or 1. Bit operations change and test single bits: that's how permissions, settings and colours are stored.
12 in binary is 00001100 = 8 + 4. Each position is a power of two: 128, 64, 32, 16, 8, 4, 2, 1.
Long binary strings are hard to read, so the hexadecimal system is used: one digit 0–F is exactly 4 bits.
| decimal | binary | hexadecimal |
|---|---|---|
| 12 | 0000 1100 |
0x0C |
| 255 | 1111 1111 |
0xFF |
| 31 | 0001 1111 |
0x1F |
In C code a hexadecimal number is written with 0x and printed with %X: printf("%X", 255u) prints FF.
Use unsigned int for bit operations: it has no negative values, and all 32 bits are simply the number. With int, a shift into the sign bit is undefined behaviour: CI with UBSan stops 1 << 31 with left shift of 1 by 31 places cannot be represented in type 'int'. Write 1u << 31: the u makes the constant unsigned.
| operation | what it does | 12u and 10u |
|---|---|---|
a & b |
1 where both are 1 | 00001000 = 8 |
a | b |
1 where at least one is 1 | 00001110 = 14 |
a ^ b |
1 where exactly one is 1 | 00000110 = 6 |
~a |
flips every bit | 11110011 (lowest 8) |
a << 1 |
shifts left: × 2 | 00011000 = 24 |
a >> 2 |
shifts right: / 4 | 00000011 = 3 |
& and && are different things: & works on each bit, && on the whole condition (true or false). The same goes for | and ||.
A mask is a number with only the bits of interest switched on. For example permissions (perms): READ = 1u (001), WRITE = 2u (010), EXECUTE = 4u (100).
| to | how |
|---|---|
| switch a bit on | perms |= WRITE; |
| switch a bit off | perms &= ~WRITE; |
| test a bit | if (perms & WRITE) |
| toggle a bit | perms ^= WRITE; |
One more common move is pulling out a byte: (x >> 8) & 0xFFu shifts the second byte to the start and keeps only its 8 bits. In the homework you'll split a colour this way.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.