By the end of this lesson, cadets will be able to:
Complete before class. The reading for this lesson is a short set of course notes, not a Beej chapter. It pulls the applied techniques together in one place.
| Source | What it covers |
|---|---|
| Lesson 35 notes: Applied Bitwise Operators | Masks, the extract / replace / pack idioms, single-bit flag tricks, the precedence trap, and a note on struct bit-fields. About 10 minutes. |
| Lesson 34 notes (review) | If the six operators or two’s complement feel rusty, skim last lesson’s material first. |
Lesson 34 was about what each bitwise operator does to individual bits. Today is about using them together to work with fields: named groups of bits packed inside one integer. The overview below is a quick reference; the full treatment is in the Lesson 35 notes, and the hands-on practice is in Section 5.
The Lesson 35 deck walks through masks, the extract / replace / pack idioms, the RGB565 layout, and the precedence traps. The slides are the spine of the lecture; the widgets are where you practice.
A field is a group of bits inside an integer that means something on its own. RGB565 packs a color into a 16-bit unsigned short as three fields:
A mask is an integer whose bits you set deliberately so that & or | against it has a known effect. A field mask is the all-ones value for a field width, shifted into the field’s position. The green mask, six 1s at bits 10–5, is 0x3F << 5.
| Move | Pattern | Green field example |
|---|---|---|
| Extract | shift, then mask | (color >> 5) & 0x3F |
| Replace | clear, then OR | (color & ~(0x3F<<5)) | ((g&0x3F)<<5) |
| Pack | mask, shift, OR | ((r&0x1F)<<11) | ((g&0x3F)<<5) | (b&0x1F) |
Mask each incoming value with its field mask before you shift it into place. That one guard keeps an out-of-range value from spilling into a neighbor field, and the lab’s hidden tests check for exactly that habit.
When a field is a single bit (a flag), the moves shrink to one-liners: test with flags & m, set with flags |= m, clear with flags &= ~m, toggle with flags ^= m. You will meet these in permission bytes, hardware registers, and protocol headers.
The trap to remember: bitwise operators have low precedence, and comparison operators outrank &. So flags & MASK == 0 is parsed as flags & (MASK == 0), which is almost never what you meant. Always parenthesize: (flags & MASK) == 0. And keep & (bitwise) distinct from && (logical).
This lesson introduces no new sections of the DFCS C Programming Standard. The lab reinforces habits you already hold yourself to: the named field constants in color.h keep magic numbers out of your code (§7.1), those constants follow the SCREAMING_SNAKE_CASE convention for macros (§5.4 / §5.6), and your functions and variables stay in snake_case (§5.1). As always, the build runs with -Wall -Werror, so a warning is a failure, not a suggestion. The full standard lives in the course Resources section.
Three things to do in class. Use the checklist to track your progress. The two widgets each give you a completion code for the Lesson 35 reflection.
Ten tasks across four sections. Type each expression at the prompt; the right pane shows the color word, the result, and how the fields decode. The variable color is pre-loaded.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection.
unsigned short holds red in the top 5 bits (15–11), green in the middle 6 bits (10–5), and blue in the low 5 bits (4–0). The variable color is pre-loaded. Type any expression using color, numbers (decimal or 0x hex), and the operators & | ^ ~ << >>. Parentheses work. Remember C precedence: shifts bind tighter than &, which binds tighter than |.
Eight questions on the patterns you meet in real systems and security code. Answer each, submit, and move on. The score card gives you your completion code.
Enter your name. It will appear with your completion code on the score card so you can include it in your screenshot for the Blackboard reflection.