Bitwise Manipulation & Masks (OCR A Level Computer Science): Revision Note

James Woodhouse

Written by: James Woodhouse

Reviewed by: Lucy Kirkham

Updated on

What are Logical Shifts?

  • Logical shifts are the process of moving the bits in a binary number to the left or right by a specified number of places

  • Bitwise manipulation uses logical operators like AND, OR, XOR, and NOT to manipulate binary numbers

Logical Shifts

  • Logical binary shifts are operations performed on binary numbers where all the bits in the number are moved left or right by a specified number of positions

  • These shifts are commonly used in computer programming and digital systems

  • There are two types of logical binary shifts: Left and Right

Example left shift

The following number is shifted by two places to the left. 

Example Left shift

Original number:     00001110 = 14

Left shift (2) result: 00111000 = 56

Each left shift has doubled the number:

  • Original value = 14

  • Left shift 1 - Doubled the number to 28

  • Left shift 2 - Doubled the number to 56

Example right shift

The following number is shifted by three places to the right. 

Example Right Shift

Original number:       11001000 = 200

Right shift (3) result: 00011001 = 25

Each right shift has halved the number:

  • Original value = 200

  • Right shift 1 - Halved the number to 100

  • Right shift 2 - Halved the number to 50

  • Right shift 3 - Halved the number to 25

Bitwise Manipulation

Bitwise AND operation

If both bits are 1 in the binary number and the mask, the result will be 1. Otherwise, the result will be 0.

Description

128

64

32

16

8

4

2

1

Binary 

1

0

1

1

1

0

0

1

Mask

0

0

1

1

0

0

0

0

Result

0

0

1

1

0

0

0

0

Bitwise OR operation

If either bit is 1 in the binary number or the mask, the result will be 1. Otherwise, the result will be 0.

Description

128

64

32

16

8

4

2

1

Binary 

1

1

0

0

1

0

1

0

Mask

0

1

1

1

0

0

0

0

Result

1

1

1

1

1

0

1

0

Bitwise XOR operation

If only 1 of the bits is 1 in the binary number or the mask, the result will be 1. Otherwise, the result will be 0.

Description

128

64

32

16

8

4

2

1

Binary 

1

0

1

0

1

0

1

0

Mask

0

0

1

1

0

0

0

0

Result

1

0

0

1

1

0

1

0

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.

Lucy Kirkham

Author: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.