4.1.19

Bitwise Manipulation - Masks

Test yourself

Bitwise Manipulation - Masks

Bitwise manipulation is the act of modifying data by changing the bits within individual bytes.

Illustrative background for MasksIllustrative background for Masks ?? "content

Masks

  • Masks can be used with binary numbers to extract, set or toggle certain bits within a byte.
  • The following examples use the mask 1 1 1 1 0 0 0 0.
Illustrative background for ANDIllustrative background for AND ?? "content

AND

  • The AND operation extracts whichever bits of the byte correspond to the 1s in the mask. The bits that correspond to 0 are returned as 0.
  • For example, our AND mask extracts just the first four bits of a byte:
    • Original byte: 1 0 1 1 1 0 0 1
    • AND Mask: 1 1 1 1 0 0 0 0
    • Result: 1 0 1 1 0 0 0 0
Illustrative background for ORIllustrative background for OR ?? "content

OR

  • The OR operation sets digits equal to 1 (for bits corresponding to the 1s in the mask) or leaves them unchanged (for bits corresponding to the 0s in the mask).
  • For example, our OR mask sets the first four bits of a byte to ‘1’:
    • Original byte: 1 0 1 1 1 0 0 1
    • OR Mask: 1 1 1 1 0 0 0 0
    • Result: 1 1 1 1 1 0 0 1
Illustrative background for XORIllustrative background for XOR ?? "content

XOR

  • The XOR operation toggles binary digits (for bits corresponding to the 1s in the mask) or leave them unchanged (for bits corresponding to the 0s in the mask).
  • For example, our XOR mask toggles the first four bits of a byte:
    • Original byte: 1 0 1 1 1 0 0 1
    • XOR Mask: 1 1 1 1 0 0 0 0
    • Result: 0 1 0 0 1 0 0 1

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Ahead

6.3Thinking Procedurally

6.4Thinking Logically

6.5Thinking Concurrently

7Problem Solving & Programming

8Algorithms

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium