A fixed-width NOT flips every bit in the word. For an 8-bit word, NOT 00001111 is 11110000. The width matters: an unlimited mathematical complement and an 8-bit complement are different models.
In C, logical operators treat zero as false and every nonzero value as true. Their result is 0 or 1. For example, 2 && 4 is 1, but 2 & 4 is 0. Logical && and || also short-circuit evaluation.
De Morgan’s law connects AND, OR, and NOT: ~(a & b) equals (~a | ~b) at the same width. You can prove it for one bit with a truth table, then apply it to each column of a word.
Explain it before you practise
Look away from the example. Explain why it works and predict a boundary case. Then check your reasoning.
Predict 2 & 4 and 2 && 4. Why can their answers differ even though both inputs are nonzero?
Check your reasoning
2 & 4 is 0 because 00000010 and 00000100 share no set position. 2 && 4 is 1 because C logical AND checks whether both whole values are nonzero.
Cement this knowledge
Solve ten fresh questions without the worked example. Mistakes return during the session; spaced review helps you recall the method on later days.
Practice this topic →