Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/01 Computer systems/Data Types/Bitwise manipulation and masks

Bitwise manipulation and masks: shifts, combining with AND, OR and XOR

Bitwise manipulation and masks: shifts, combining with AND, OR, and XOR

1.4.1 Data Types

Data Types — Note 9 of 10

Candidates should be able to perform right and left logical shifts. Candidates should understand the effect of right and left shifts on a binary numbers. Candidates should understand the purpose of using masks with bitwise operators, and should have experience of applying masks using AND, OR and XOR.

What you need to know

Bitwise operations work on the individual bits of a binary value. Shifts move bits left or right, and masks combined with AND, OR or XOR can check, clear, set or toggle particular bits.

What to know

  • A left shift of n places multiplies an unsigned value by 2^n; a right shift of n places divides by 2^n (rounding down).
  • Uses: extracting part of a value (e.g. the lower nibble), setting or clearing status flags, checking whether a number is odd (AND with 00000001).

Key terms

Exam tipCommon mistake

Logical shift

All bits move left or right by a number of places; bits shifted out are lost and the empty positions are filled with 0s.

Mask

A binary pattern combined with a value using a bitwise operator to select or change particular bits.

  • AND with a mask: CLEARS bits where the mask is 0 and KEEPS bits where the mask is 1. Used to check or extract bits.
  • OR with a mask: SETS bits to 1 where the mask is 1, and leaves the others unchanged.
  • XOR with a mask: TOGGLES (flips) bits where the mask is 1, and leaves the others unchanged.
  • Exam tip: Write the value and mask directly above each other and work through column by column.

Exam technique

  • Mistake:Forgetting that bits shifted off the end are lost, which can change the result unexpectedly.

Code examples

Masks with AND, OR and XOR
text
Value       1 0 1 1 0 1 1 0
AND mask    0 0 0 0 1 1 1 1   -> 0 0 0 0 0 1 1 0  (keep lower nibble)
OR  mask    1 0 0 0 0 0 0 0   -> 1 0 1 1 0 1 1 0  (set the MSB)
XOR mask    1 1 1 1 1 1 1 1   -> 0 1 0 0 1 0 0 1  (flip every bit)

Shift left 1:   0 1 1 0 1 1 0 0  (MSB lost)
Shift right 2:  0 0 1 0 1 1 0 1
← Previous noteFloating point arithmetic: addition and subtraction1.4.1 Data TypesNext note →How character sets (ASCII and UNICODE) are used to represent text1.4.1 Data Types

Back to Data Types

Data Types

Note 9 of 10

  1. 1Primitive data types: integer, real/floating point, character, string and Boolean
  2. 2Represent positive integers in binary
  3. 3Sign and magnitude and two's complement to represent negative numbers
  4. 4Addition and subtraction of binary integers
  5. 5Represent positive integers in hexadecimal
  6. 6Convert positive integers between binary, hexadecimal and denary
  7. 7Representation and normalisation of floating point numbers in binary
  8. 8Floating point arithmetic: addition and subtraction
  9. 9Bitwise manipulation and masks: shifts, combining with AND, OR and XOR
  10. 10How character sets (ASCII and UNICODE) are used to represent text
Topic overview