Photo AI

Last Updated Sep 27, 2025

Bitwise Manipulation & Masks Simplified Revision Notes

Revision notes with simplified explanations to understand Bitwise Manipulation & Masks quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

381+ students studying

Bitwise Manipulation & Masks

Overview

Bitwise manipulation involves working directly with the binary representation of data. This is often used in low-level programming tasks, such as optimising performance or controlling hardware. Masks are specific binary patterns applied using bitwise operators to isolate, modify, or combine certain bits within binary data. Understanding bitwise operations and how to apply masks is essential for efficient programming and problem-solving in Computer Science.

Bitwise Operators

Bitwise operators perform operations on individual bits of binary numbers.

AND (&****):

  • Compares corresponding bits of two binary numbers.
  • Result is 1 if both bits are 1; otherwise, 0.
  • Use: Isolate specific bits using masks.
lightbulbExample

Example:

1101  (13)
& 1011  (11)
------
1001  (9)

OR (|****):

  • Compares corresponding bits of two binary numbers.
  • Result is 1 if at least one bit is 1; otherwise, 0.
  • Use: Set specific bits to 1.
lightbulbExample

Example:

1101  (13)
| 1010  (10)
------
1111  (15)

XOR (^****):

  • Compares corresponding bits of two binary numbers.
  • Result is 1 if the bits are different; otherwise, 0.
  • Use: Toggle specific bits (flip between 0 and 1).
lightbulbExample

Example:

1101  (13)
^ 1010  (10)
------
0111  (7)

NOT (~****):

  • Inverts all bits (0 becomes 1, 1 becomes 0).
  • Use: Bitwise negation (not widely used with masks).

Bit Shifts

Shifts move all bits in a binary number to the left or right.

Left Shift (<<****):

  • Moves bits to the left by a specified number of positions.
  • Inserts 0s on the right.
  • Effectively multiplies the number by (2^n), where (n) is the number of shifts.
lightbulbExample

Example:

0011 << 2 = 1100

Right Shift (>>****):

  • Moves bits to the right by a specified number of positions.
  • Inserts 0s on the left.
  • Effectively divides the number by (2^n) (integer division).
lightbulbExample

Example:

1100 >> 2 = 0011

Bit Masks

A mask is a binary pattern used with bitwise operators to manipulate specific bits in another binary number.

Purpose of Masks:

  • AND Mask: Isolate specific bits.
  • OR Mask: Set specific bits to 1.
  • XOR Mask: Toggle specific bits.

Examples

lightbulbExample

Example 1: Using an AND Mask to Isolate Bits Isolate the last 4 bits of the number 10111011 using an AND mask.


  • Number: 10111011
  • Mask: 00001111 (AND mask to isolate last 4 bits)

Operation:

10111011
& 00001111
----------
  00001011


Result:

1011 (decimal 11).

lightbulbExample

Example 2: Using an OR Mask to Set Bits Set the last 3 bits of 10100000 to 1.


  • Number: 10100000
  • Mask: 00000111 (OR mask to set last 3 bits)

Operation:

10100000
| 00000111
----------
  10100111


Result:

10100111.

lightbulbExample

Example 3: Using an XOR Mask to Toggle Bits Toggle the last 4 bits of 11110000.


  • Number: 11110000
  • Mask: 00001111 (XOR mask to toggle last 4 bits)

Operation:

11110000
^ 00001111
----------
  11111111


Result:

11111111.

lightbulbExample

Example 4: Left Shift Perform a left shift of 2 positions on 00001101.


Operation:

00001101 << 2 = 00110100


Result:

00110100 (decimal 52).

lightbulbExample

Example 5: Right Shift Perform a right shift of 3 positions on 10110000.


Operation:

10110000 >> 3 = 00010110


Result:

00010110 (decimal 22).

Note Summary

infoNote

Common Mistakes

  • Forgetting to Align Bits in Masks: Ensure masks are aligned correctly for the operation (e.g., isolating specific bits).
  • Incorrect Shift Interpretation: Remember that left shifts multiply and right shifts divide, and this applies only to unsigned binary.
  • Misunderstanding XOR: XOR toggles bits, so reapplying the same XOR mask will revert the changes.
infoNote

Key Takeaways

  • Bitwise Operators:
  • AND: Isolate bits.
  • OR: Set bits to 1.
  • XOR: Toggle bits.
  • Shifts:
  • Left shifts multiply by 2n2^n
  • Right shifts divide by 2n2^n
  • Masks: Allow precise control over specific bits using bitwise operators, useful for low-level programming and optimisation.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Bitwise Manipulation & Masks

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

90 flashcards

Flashcards on Bitwise Manipulation & Masks

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

9 quizzes

Quizzes on Bitwise Manipulation & Masks

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Bitwise Manipulation & Masks

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Bitwise Manipulation & Masks

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Bitwise Manipulation & Masks

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Bitwise Manipulation & Masks you should explore

Discover More Revision Notes Related to Bitwise Manipulation & Masks to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Data Types

Primitive Data Types

user avatar
user avatar
user avatar
user avatar
user avatar

246+ studying

184KViews

96%

114 rated

Data Types

Positive Binary Numbers

user avatar
user avatar
user avatar
user avatar
user avatar

335+ studying

196KViews

96%

114 rated

Data Types

Negative Binary Numbers

user avatar
user avatar
user avatar
user avatar
user avatar

326+ studying

191KViews

96%

114 rated

Data Types

Binary Addition & Subtraction

user avatar
user avatar
user avatar
user avatar
user avatar

377+ studying

188KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered