Bitwise and Special Operators
Shifts, AND, OR, XOR, complement, plus the comma operator and sizeof as an operator.
Below the Numbers
Chapter 2 established that every value is bits in memory. The bitwise operators let you work at that level directly: six operators that treat an integer not as a quantity but as a row of individual bits. They power hardware registers, flags, permissions, compression, and a reliable section of every C exam. Alongside them this lesson covers two operators that fit no family: the comma and sizeof.
Six Bitwise Operators
Working on the bits of integer operands:
&AND: result bit is 1 where both inputs are 1.|OR: result bit is 1 where either input is 1.^XOR: result bit is 1 where the inputs differ.~NOT: flips every bit.<<left shift: slide bits toward the high end, filling with zeros.>>right shift: slide bits toward the low end.
Watch them work on small numbers. Twelve is binary 1100, ten is 1010:
AND keeps only the bits both share (1000, eight), OR merges them (1110, fourteen), XOR keeps the disagreements (0110, six). Working these by hand, write the operands in binary, apply the rule column by column, is a mechanical skill worth practicing until boring; exams grade it and real debugging uses it.
Do not confuse & with && or | with ||: the single-character forms combine bits, the doubled forms combine truth values. 12 & 10 is 8; 12 && 10 is 1. Both compile, which makes the typo quiet and the distinction examinable.
A second mistranslation worth heading off now: ^ is XOR, not exponentiation. C has no power operator at all, so 2 ^ 8 is a bitwise XOR that evaluates to 10 rather than 256. Raising a value to a power is pow(2.0, 8.0) from the math library, which arrives later in this chapter. The mistake is common enough that GCC ships a warning for it, -Wxor-used-as-pow, which asks whether you meant 1 << 8.
Shifts: Doubling and Halving
Shifting left by one appends a zero bit, which multiplies by 2; shifting right by one drops the lowest bit, which divides by 2, discarding the remainder:
So n << 3 multiplies by 8, and n >> 2 divides by 4. The last line shows the most used single-bit trick in C: n & 1 isolates the lowest bit, which is 1 exactly for odd numbers, an even/odd test that sits beside n % 2 from earlier in the chapter.
Two safety rules the standard imposes, both exam-worthy: shifting by a negative amount or by the type's width or more is undefined behaviour (n << 32 on a 32-bit int has no defined result), and these lessons shift non-negative values only, because right-shifting a negative number gives an implementation-defined result and left-shifting one is undefined behaviour from C99 onward (C89 itself defined it by the bit pattern, but treat it as off-limits either way). Stick to non-negative operands and modest shift counts and every result is exact.
~ flips all bits at once. On unsigned values its result is well defined: ~0u is an unsigned int with every bit set, the largest value the type holds, which ties back to UINT_MAX from chapter 2.
The Comma Operator
The comma operator evaluates its left operand, discards the result, then evaluates and yields its right operand: x = (3, 5); stores 5. Stated like that it sounds useless, and in beginner code it nearly is; its one respectable home is the for loop header you will meet in chapter 6, where it lets two updates ride in one slot. What matters now is recognition: the commas separating function arguments and declarations are not this operator, and when an exam presents (a, b, c) as an expression, its value is c.
sizeof, Formally
You used sizeof in chapter 2 to measure types; it is in fact a full operator, one of the 32 keywords, and it also accepts expressions: sizeof total reports the size of total's type, parentheses optional for expressions, required for type names like sizeof(int). Its result is computed at compile time from types alone, and the operand is not evaluated. It never causes the expression inside it to run.
Key Takeaways
& | ^ ~ << >>operate on bits; work examples in binary, column by column.&vs&&and|vs||: bits versus truth values, both legal, easily swapped by accident.^is XOR, not exponentiation:2 ^ 8is 10, and C has no power operator, onlypowin the math library.n << kmultiplies by 2 to the k;n >> kdivides, discarding remainder;n & 1tests oddness.- Shifting by the type's width or more, or by negative amounts, is undefined behaviour; keep operands non-negative and counts small.
- The comma operator yields its right operand; its legitimate use arrives with for loops.
sizeofis an operator, works on types and expressions, and never evaluates its operand.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Bitwise and Special Operators - Quiz
Test your understanding of the lesson.
Practice Exercises
The Bit Report
Read a non-negative integer and print three lines: the number shifted left by one (doubled), shifted right by one (halved), and its lowest bit isolated with & 1, which reveals whether it is odd.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!