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.
Logical shift Every bit moves one place, a 0 fills the gap and the bit pushed off the end is lost: left doubles the value, right halves it.
1 more simulation for this topic — run them in the Simulations section below
Simulations
Every simulation here runs the real model — try the steps on a card, then check what you see against the notes.
1 simulation
- GeoGebraCore9618 4.3
8-bit binary place-value table
An 8-bit table with place values 128 down to 1. Tap bits to set a pattern, then press the ×2 and ÷2 buttons and read the decimal total.
Try this
- Tap bits to make 00010110, which is 22.
- Press ×2: every 1 moves one column to the left and the total doubles to 44. That is a logical shift left by one place.
- Press ÷2 twice: every 1 moves one column right each time and the total halves, 44 to 22 to 11. That is a logical shift right.
Look for Shifting left one place multiplies by 2 and shifting right divides by 2, because each column is worth twice the column on its right.
D Stark · GeoGebra · GeoGebra Terms of Service
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
Key terms for this topic — skim now; the Check step will test them.
- Arithmetic Shift Right (ASR)
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.
- 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.
- Logical Shift Right (LSR)
An operation that shifts all bits in a binary number one or more positions to the right. The least significant bit(s) are discarded, and the most significant bit position(s) are filled with 0s.
- Mask
A binary value (a bit pattern) used with a logical operator (AND, OR, XOR) to modify another binary value. The mask determines which bits are affected.
Name it
Read the meaning, then pick which of this lesson’s terms it describes. Miss one and you see what your choice really means.
A shift operation for signed (two's complement) numbers. It shifts bits to the right, but the most significant bit (the sign bit) is copied into the newly vacant position, preserving the number's sign.
Quick check
Write your answer first, then compare it with the model one — the gap is what you would have lost.
Teach it back
If you can explain it simply, you own it — gaps here are marks you’d lose.
Teach it back
Explain this topic as if teaching a friend. We name the gaps an examiner would still dock.
Revision flashcards
Guess first, then flip — retrieval beats re-reading.
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.
Discuss Bit manipulation
Ask, share and discuss with other Computer Science students