By the end of this lesson, cadets will be able to:
Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 24 (Bitwise Operations) | The six operators and how each one works on bits |
| King, C Programming: A Modern Approach | Chapter 20 (Low-Level Programming), sections on bitwise operators | Optional deeper dive into shifts and bit manipulation |
& AND, | OR, ^ XOR, ~ NOT<< and right shift >>& and logical &&Back in Lesson 2 you saw that the same bits read differently as signed and unsigned. This lesson goes underneath that: the rule the machine uses to build negative numbers, and the operators that let you read and change bits one at a time. The reference material below covers what you will see in lecture and use in both walkthroughs.
The Lesson 34 deck covers the six operators, two's complement, the shifts, and the four single-bit idioms. The slides are the spine of the in-class lecture; the two walkthroughs are where you practice each idea hands-on.
Open Lesson 34 slides → replace href with your SharePoint link
Most of the time you treat an integer as a single number. Sometimes it is more useful to treat it as a row of 16 or 32 independent switches. A single integer can track which permissions a user has, which options are turned on, or which sensors reported in, one bit per flag. Bitwise operators are how you read and flip those switches without disturbing the others.
This lesson is the fundamentals: the operators and the four basic moves. Lesson 35 builds on it with multi-bit masks and packing several values into one integer. Get the single-bit moves solid here and the next lesson is mostly review with bigger masks.
Five take two operands; ~ takes one. They work bit by bit, position by position.
| Operator | Name | A result bit is 1 when... |
|---|---|---|
| & | AND | both input bits are 1 |
| | | OR | either input bit is 1 |
| ^ | XOR | the two input bits differ |
| ~ | NOT | the single input bit is 0 (it flips every bit) |
| << | left shift | a bit slid into this position from the right (zeros fill in) |
| >> | right shift | a bit slid into this position from the left (low bits fall off) |
Watch the doubles. & and && are different operators. Single & combines bits; double && is the logical AND that produces true or false. Same for | versus ||. Using the wrong one is a common and quiet bug.
Shifts as multiply and divide. On an unsigned value, x << n multiplies by 2n and x >> n divides by 2n (dropping the remainder), as long as nothing important falls off the end. We work in unsigned short in this lesson so the right shift is well defined; on signed values the behavior of >> is not guaranteed.
Two's complement is the rule almost every machine uses to represent signed integers. The headline: to negate a number, invert every bit and add one. That is the entire rule.
The sign bit is a weight, not a flag. In a 16-bit signed value the top bit does not just mean negative; it carries the weight -215 = -32768. Every other bit adds its usual positive value. So 1111111111111111 is -32768 + 32767 = -1, and the all-ones pattern is always -1 in a signed type.
The range is lopsided. A short runs from -32768 to 32767. There is one more negative value than positive because zero takes one of the non-negative slots. That is why negating the most negative number gives you back the most negative number: its positive twin does not exist. The two's complement walkthrough in Section 5 lets you watch all of this happen bit by bit.
Almost everything you do with bits comes down to four moves on one bit at position n. Each one builds a single-bit mask with (1 << n) and combines it with the value. Memorize these four; the lab is built on them.
| Move | Expression | Why it works |
|---|---|---|
| Set | value | (1 << n) | OR with a 1 forces that bit on; OR with the 0s elsewhere leaves them alone |
| Clear | value & ~(1 << n) | the mask is all 1s except bit n; AND forces only that bit off |
| Toggle | value ^ (1 << n) | XOR with a 1 flips that bit; XOR with the 0s elsewhere leaves them alone |
| Test | (value >> n) & 1 | slide bit n down to position 0, then mask it off; result is 1 if set, 0 if not |
This lesson does not introduce new DFCS C Programming Standard rules, but the lab is a good place to keep the naming rules sharp. The perk flags are #define constants in SCREAMING_SNAKE_CASE (§5.6), the functions and variables are snake_case (§5.1, §5.2), and naming each perk with a #define instead of a bare number is exactly the rule against magic numbers (§7.1). The full standard is in the course Resources section.
Class opens with Quiz 10 (Recursion II and III). After the quiz, work through both walkthroughs and then the lab. Use the checklist to track your progress.
unsigned short. Auto-graded through GitHub Classroom; accept the assignment with the GitHub Classroom invite. Worth 2 points.
Nine tasks across four sections. Build a negative number by hand, see why the high bit is a negative weight, and watch the range run one slot further negative than positive. When you finish, copy the completion code for the Lesson 34 reflection.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 34 reflection.
short or unsigned short to see its 16-bit pattern, plus how it reads as signed and unsigned. You can also negate by hand: type ~x to invert every bit (one's complement), or ~x + 1 to finish the negation. The sign bit (the highest bit of a signed type) is outlined in red.
Twelve tasks across three sections. Run each of the six operators, then build the four single-bit idioms (set, clear, toggle, test) yourself. When you finish, copy the completion code for the Lesson 34 reflection.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 34 reflection.
unsigned short, then combine values with AND, OR, XOR, NOT, and the two shifts. The right pane shows both operands and the result, bit for bit. The last section drills the four moves you will use constantly: set, clear, toggle, and test a single bit.unsigned short x = 42;, then type an expression. Operators: & AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shift. You can combine them: x | (1 << 3). Operands can be variables or numbers. All values are 16-bit unsigned.