In simple terms
A friendly intro before the formal notes — no formulas yet.
Controlling the Code's DNA
Bit manipulation is the art of directly altering the individual 1s and 0s that make up data in a computer. This gives programmers fine-grained control for maximum efficiency and is crucial for tasks like graphics, networking, and systems programming.
Imagine a long panel of light switches, where each switch represents a bit. Bit manipulation is like being able to perform specific actions on these switches without looking at the whole panel. A 'shift' is like telling a friend to move every switch's state one position to the left. Using an 'AND mask' is like putting a cardboard cutout over the panel with holes only for the switches you want to check are 'ON'. Using an 'OR mask' is like using a tool that can reach through specific holes to flick switches 'ON', and an 'XOR mask' uses a tool to flip the current state of selected switches.
- 1
Logical shifts move all bits left or right, filling vacant spots with zeros. This is a fast way to multiply or perform integer division by powers of 2.
- 2
An AND mask is used to clear bits or check if a bit is set. By ANDing a value with a mask, only the bits that are '1' in both the value and the mask remain '1'.
- 3
An OR mask is used to set bits to '1', while an XOR mask is used to toggle (flip) bits. These are essential for managing status flags or setting permissions.
- 4
For signed integers in two's complement, an Arithmetic Shift Right (ASR) is used. It preserves the sign bit to maintain the number's positivity or negativity during division.
Explore the concept
Use the live diagram and synced steps — play it or tap a step card to walk through.
Full topic notes
Formal explanation with the rigour you need for the exam.
Bitwise Shifts
A bitwise shift moves every bit in a register (a storage location, like a byte) to the left or right by a specified number of positions. Bits that are shifted off one end are discarded, and the empty spaces at the other end are filled in according to specific rules. These operations are extremely fast for a processor to execute and provide a highly efficient way to perform multiplication and division by powers of two.
Logical Shifts (LSL and LSR)
Logical shifts are the simplest type of shift. They treat the number as a simple sequence of bits, regardless of whether it represents a signed or unsigned number. When shifting, the empty positions are always filled with zeros.
Logical Shift Left (LSL): All bits shift left. The most significant bit (MSB) is discarded. The least significant bit (LSB) position is filled with a 0. Effect: multiplication by for shifts.
Logical Shift Right (LSR): All bits shift right. The least significant bit (LSB) is discarded. The most significant bit (MSB) position is filled with a 0. Effect: integer division by for shifts.
Arithmetic Shifts (ASR)
When working with signed numbers stored in two's complement format, a logical shift right can produce incorrect results for negative numbers. For example, shifting a negative number right with LSR would introduce a 0 at the MSB, making the number positive. To solve this, we use an Arithmetic Shift Right (ASR). This shift preserves the sign of the number by copying the original sign bit into the vacant positions. Note that an Arithmetic Shift Left is identical in operation to a Logical Shift Left.
Masking and Logical Operations
Masking is a technique used to access or modify specific bits within a larger binary value, like a byte. It works by combining the value with a special bit pattern, called a mask, using a bitwise logical operator. The three key operators are AND, OR, and XOR.
AND (&): Used to clear bits or check if a bit is set. (clears the bit). (leaves the bit unchanged).
OR (|): Used to set bits. (sets the bit). (leaves the bit unchanged).
XOR (^): Used to toggle (flip) bits. (flips the bit). (leaves the bit unchanged).
Worked examples
See the formulas applied — reveal one step at a time, like the exam.
An 8-bit register contains the binary value 00110101. What is the result, in binary and denary, after applying: a) LSL 2, b) LSR 3?
- 1
The original value 00110101 is in denary.
An 8-bit register stores a signed integer in two's complement. It holds the value 11101100. What is the result of applying an ASR 2? Give your answer in binary and state the denary equivalent before and after.
- 1
The original value is 11101100.
- As it's a negative number (MSB is 1), we find its magnitude by flipping the bits and adding 1: . This is . So the original value is -20.
A byte variable, STATUS, holds the value 10010110. Bits represent flags (from right, bit 0): Bit 1: , Bit 4: ERROR, Bit 7: ENABLED. Perform the following operations, showing the mask and the result in binary. a) Clear the ERROR flag (set bit 4 to 0). b) Set the ENABLED flag (set bit 7 to 1). c) Toggle the flag (flip bit 1).
- 1
Original STATUS byte: 10010110
How it all connects
The big idea sits in the middle — tap a linked idea to explore the link.
Tap a linked idea to see how it connects back to the main topic — that connection is what examiners reward.
Glossary
Try to recall each definition before you reveal it.
Quick check
Answer in your head first — then tap to check. No pressure.
Revision flashcards
Flip the card. Test yourself before the exam.
What is a Logical Shift Left (LSL)?
An operation that shifts all bits in a binary number one or more positions to the left. The most significant bit(s) are discarded, and the least significant bit position(s) are filled with 0s.
Key takeaways
Review these before you close the topic — retrieval beats re-reading.
- ✓
Logical Shift Left (LSL): All bits shift left. The most significant bit (MSB) is discarded. The least significant bit (LSB) position is filled with a 0. Effect: multiplication by for shifts.
- ✓
Logical Shift Right (LSR): All bits shift right. The least significant bit (LSB) is discarded. The most significant bit (MSB) position is filled with a 0. Effect: integer division by for shifts.
Practice — then mark it
The whole point: a real Cambridge question, marked mark-by-mark.
Bit Manipulation
Bit Manipulation
Extra simulations & links
PhET, GeoGebra and other curated tools — open in a new tab.
Frequently asked
Checkpoint
One marked question is worth ten re-reads — close the loop before you move on.
Reading it isn’t knowing it — prove it.
Before you move on: do Bit Manipulation on paper, snap a photo, and get examiner-style feedback on exactly where you win and lose marks.