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.
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.
2^n; a right shift of n places divides by 2^n (rounding down).00000001).Exam tipCommon mistake
All bits move left or right by a number of places; bits shifted out are lost and the empty positions are filled with 0s.
A binary pattern combined with a value using a bitwise operator to select or change particular bits.
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