Bitwise operators and shifts
Bitwise operators work on the individual binary digits of integer values. They are useful for compact sets of flags and low-level data. A bit mask is an integer whose selected bits describe which positions to inspect or change.
const int packed = 0b001;const int labelled = 0b010;int flags = packed | labelled;System.Console.WriteLine(flags);System.Console.WriteLine((flags & labelled) == labelled);The low two positions represent independent conditions.
Expected output (reasoned, not executed):
3TrueRead binary masks
Binary literals begin with 0b. The rightmost bit represents 1, the next 2, the next 4, then 8. & keeps positions set in both operands, | sets positions present in either, ^ toggles positions present in exactly one, and ~ complements every bit. Here masks are int values, so complement affects all 32 bits, not just displayed digits.
Set, test and clear flags
Use flags |= mask to set bits, (flags & mask) == mask to test that all selected bits are set, flags &= ~mask to clear bits, and flags ^= mask to toggle. (flags & mask) != 0 tests whether any selected bit is set. Use parentheses around bitwise operations before comparisons.
Shift positions
<< shifts left, discarding high bits that do not fit. >> shifts signed int right with sign extension; >>> shifts right with zero fill (C# 11 or later). With int, only the low five bits of the shift count are used, so shifting by 32 is like shifting by zero. Bitwise shifts do not throw on overflow in checked contexts. Use unsigned types when that better expresses raw bits.
Examples and walkthroughs
int flags = 0b111;flags &= ~0b010;System.Console.WriteLine(flags);flags ^= 0b001;System.Console.WriteLine(flags);Clearing position 1 produces binary 101; toggling position 0 produces 100.
Expected output (reasoned, not executed):
54int n = -8;System.Console.WriteLine(3 << 2);System.Console.WriteLine(n >> 1);System.Console.WriteLine(n >>> 1);System.Console.WriteLine(1 << 32);Left shift moves bits two places. Arithmetic right shift preserves the sign; logical right shift fills with zeros. The last count is masked to zero.
Expected output (reasoned, not executed):
12-421474836441Common mistake and correction
(flags & mask) != 0 is not an all-bits test for a combined mask. For example flags 1 and mask 3 share one bit, but do not contain all required flags. Use equality with the entire mask.
Try it yourself
Use flags 1 for packed, 2 for labelled and 4 for dispatched. Set packed and labelled, test both, then clear packed and toggle dispatched. Predict every intermediate integer.
Next: If and else decisions. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bitwise operators and shifts — knowledge check
Apply the lesson concepts to the following scenarios.
What is 0b0101 & 0b0011?
How do you set a flag without clearing unrelated flags?
Which tests all mask bits?
What is 1 << 3 for int?
Which signed int right shift fills with zeros?