4.1.9

Bitwise Manipulation

Test yourself

Bitwise Manipulation - Shifts

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

Illustrative background for Logical shift rightIllustrative background for Logical shift right ?? "content

Logical shift right

  • A logical shift right moves each of the bits in a byte one place to the right.
  • The ‘least significant bit’ (LSB) on the right is lost and the empty space for the ‘most significant bit’ (MSB) is filled with a ‘0’.
    • For example, 1 0 1 0 1 0 0 0 \rightarrow 0 1 0 1 0 1 0 0
  • A logical shift right is identified with a triple chevron ‘>>>’.
Illustrative background for Logical shift leftIllustrative background for Logical shift left ?? "content

Logical shift left

  • A logical shift left moves each of the bits in a byte one place to the left.
  • The MSB on the left is lost and the empty space for the LSB is filled with a ‘0’.
    • For example: 1 0 1 0 1 0 0 0 \rightarrow 0 1 0 1 0 0 0 0
  • A logical shift left is identified with a ‘<<<’.
Illustrative background for Arithmetic shift rightIllustrative background for Arithmetic shift right ?? "content

Arithmetic shift right

  • Arithmetic shifts are almost the same as logical shifts, except the MSB is a sign bit and always remains the same.
  • An arithmetic shift right has the following result:
    • 1 0 1 0 1 0 0 0 \rightarrow 1 0 0 1 0 1 0 0
    • This has the effect of dividing the original number by 2.
  • An arithmetic shift right is identified with a '>>'.
Illustrative background for Arithmetic shift leftIllustrative background for Arithmetic shift left ?? "content

Arithmetic shift left

  • An arithmetic shift left has the following result:
    • 1 0 1 0 1 0 0 0 \rightarrow 1 1 0 1 0 0 0 0
    • This has the effect of multiplying the original number by 2.
  • An arithmetic shift left is identified with a '<<'.
Illustrative background for Circular shiftIllustrative background for Circular shift ?? "content

Circular shift

  • With a right circular shift, the same procedure applies.
  • However, instead of losing the LSB in a right shift, it ‘wraps around’ and becomes the MSB.
    • 1 0 1 0 1 0 0 0 \rightarrow 0 1 0 1 0 1 0 0
  • Similarly, with a left circular shift, the MSB becomes the new LSB.
    • 1 0 1 0 1 0 0 0 \rightarrow 0 1 0 1 0 0 0 1

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 Procedurally

6.3Thinking Logically

7Problem Solving & Programming

8Algorithms

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson