- What bit manipulation is (and why it matters)
Working with individual bits inside a byte, not just the whole number.
Most of the time a program works with whole numbers, but sometimes each individual bit of a byte carries its own meaning. Bit manipulation is the set of techniques for moving, testing and changing those single bits.
Two big jobs use it:
- Shifts — sliding all the bits left or right. This is how a processor can multiply or divide by powers of 2 very quickly, and it is also used inside graphics and encryption.
- Bit masking — using a pattern (a mask) with a bitwise operator (AND, OR, XOR) to test, set, clear or toggle chosen bits without disturbing the others.
A common real use is a status register where each bit is a separate on/off flag — for example bit 0 = 'heater on', bit 7 = 'temperature too high'. To read or change just one flag you must manipulate a single bit, which is exactly what masks are for.
| Term | Meaning |
|---|---|
| Bit 0 | the rightmost bit (least significant), place value 1 |
| MSB | most significant bit — the leftmost |
| Mask | a binary pattern chosen to pick out particular bit positions |
- Bit manipulation = working on individual bits, not the whole value.
- Shifts slide bits left/right (fast ×/÷ by powers of 2).
- Masks test/set/clear/toggle chosen bits with AND/OR/XOR.
- Bit 0 is usually the rightmost (least significant) bit.