4.1.9
Bitwise Manipulation
Bitwise Manipulation - Shifts
Bitwise Manipulation - Shifts
Bitwise manipulation is the act of modifying data by changing the bits within individual bytes.


Logical shift right
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 0 1 0 1 0 1 0 0
- A logical shift right is identified with a triple chevron ‘>>>’.


Logical shift left
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 0 1 0 1 0 0 0 0
- A logical shift left is identified with a ‘<<<’.


Arithmetic shift right
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 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 '>>'.


Arithmetic shift left
Arithmetic shift left
- An arithmetic shift left has the following result:
- 1 0 1 0 1 0 0 0 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 '<<'.


Circular shift
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 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 0 1 0 1 0 0 0 1
Bitwise Manipulation - Masks
Bitwise Manipulation - Masks
Bitwise manipulation is the act of modifying data by changing the bits within individual bytes.


Masks
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.


AND
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


OR
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


XOR
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
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction

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