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.
![Illustrative background for Logical shift right](https://image-v2.cdn.app.senecalearning.com/2020-08/bb452ae1-b220-4df3-9d02-d4cd6bc80a9e/Screenshot%202020-08-25%20at%2018.32.13-min,h_400,q_80,w_640.png)
![Illustrative background for Logical shift right ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/bb452ae1-b220-4df3-9d02-d4cd6bc80a9e/Screenshot%202020-08-25%20at%2018.32.13-min,h_400,q_80,w_640.png)
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 ‘>>>’.
![Illustrative background for Logical shift left](https://image-v2.cdn.app.senecalearning.com/2020-08/68ca329b-630c-49ad-b8f7-f63413c243c7/Screenshot%202020-08-25%20at%2018.32.20-min,h_400,q_80,w_640.png)
![Illustrative background for Logical shift left ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/68ca329b-630c-49ad-b8f7-f63413c243c7/Screenshot%202020-08-25%20at%2018.32.20-min,h_400,q_80,w_640.png)
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 ‘<<<’.
![Illustrative background for Arithmetic shift right](https://image-v2.cdn.app.senecalearning.com/2020-08/e2b98520-4fb2-4626-9b25-5f3ed6dced0f/Screenshot%202020-08-25%20at%2018.32.22-min,h_400,q_80,w_640.png)
![Illustrative background for Arithmetic shift right ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/e2b98520-4fb2-4626-9b25-5f3ed6dced0f/Screenshot%202020-08-25%20at%2018.32.22-min,h_400,q_80,w_640.png)
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 '>>'.
![Illustrative background for Arithmetic shift left](https://image-v2.cdn.app.senecalearning.com/2020-08/962d10f5-eba0-4877-9697-9fa5fe26c0fa/Screenshot%202020-08-25%20at%2018.32.23-min,h_400,q_80,w_640.png)
![Illustrative background for Arithmetic shift left ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/962d10f5-eba0-4877-9697-9fa5fe26c0fa/Screenshot%202020-08-25%20at%2018.32.23-min,h_400,q_80,w_640.png)
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 '<<'.
![Illustrative background for Circular shift](https://image-v2.cdn.app.senecalearning.com/2018-09/10d949ad-ddd7-4ae4-a908-a04169e27a2d/circle-colour-pattern-spots-,h_400,q_80,w_640.png)
![Illustrative background for Circular shift ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/10d949ad-ddd7-4ae4-a908-a04169e27a2d/circle-colour-pattern-spots-,h_400,q_80,w_640.png)
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.
![Illustrative background for Masks](https://image-v2.cdn.app.senecalearning.com/2020-07/2d9655e0-5e2b-4bf3-bf34-d765770f03ea/acting-mask-,h_400,q_80,w_640.png)
![Illustrative background for Masks ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-07/2d9655e0-5e2b-4bf3-bf34-d765770f03ea/acting-mask-,h_400,q_80,w_640.png)
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.
![Illustrative background for AND](https://image-v2.cdn.app.senecalearning.com/2020-08/f0fc434f-28d9-4057-8c28-08dfd4decdac/Screenshot%202020-08-26%20at%2008.55.55-min,h_400,q_80,w_640.png)
![Illustrative background for AND ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/f0fc434f-28d9-4057-8c28-08dfd4decdac/Screenshot%202020-08-26%20at%2008.55.55-min,h_400,q_80,w_640.png)
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
![Illustrative background for OR](https://image-v2.cdn.app.senecalearning.com/2020-08/649bffdf-b5d4-4671-9b3a-d0756abb2317/Screenshot%202020-08-26%20at%2008.55.58-min,h_400,q_80,w_640.png)
![Illustrative background for OR ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/649bffdf-b5d4-4671-9b3a-d0756abb2317/Screenshot%202020-08-26%20at%2008.55.58-min,h_400,q_80,w_640.png)
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
![Illustrative background for XOR](https://image-v2.cdn.app.senecalearning.com/2020-08/29bbdd9d-ce26-4cea-8cb2-1159ce59f37d/Screenshot%202020-08-26%20at%2008.56.00-min,h_400,q_80,w_640.png)
![Illustrative background for XOR ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/29bbdd9d-ce26-4cea-8cb2-1159ce59f37d/Screenshot%202020-08-26%20at%2008.56.00-min,h_400,q_80,w_640.png)
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
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
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