1/13
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Where are bitwise manipulations commonly used?
Quickly doubling or halving numbers
Encryption and decryption (key manipulation)
Serial communication protocols
Handling network addresses
Setting/resetting flags
Sensing flag values
Data collision detection in networks
Graphics manipulation
Compression algorithms
Logical (Unsigned) Shift Left
All digits are moved to the left.
The most significant bit (MSB) is removed and a 0 is added in place of the least significant bit (LSB).
The MSB is moved into a carry flag.
With logical shifts, the sign is not preserved.
Most of the time when you shift a binary number left, the value is doubled.
However if the MSB was 1, that value will be lost and the number won’t be doubled.
Therefore, left wise shift works for positive numbers up to 127.
Logical (Unsigned) Right Shift
All digits are moved to the right.
The least significant bit (LSB) is removed and a 0 is added in place of the most significant bit (MSB).
With logical shift right, the sign is not preserved.
As long as there is no 1 in the LSB, each right shift divides the number by 2.
Circular Shift
With a circular right shift, the LSB is copied in to the MSB and everything else is shifted right
With a circular left shift, the MSB is copied in to the LSB and everything else is shifted left
Widely used in error correction algorithms to generate a set of error correcting code words.
It is also used in serial data transfer to swap register values between two devices

Arithmetic (Signed) Right Shift
All digits are moved to the right except the MSB which stays the same in order to preserve the negative or positive state of the number.
This has the same effect as padding with a 1 for negative numbers or with a 0 for positive numbers.
The least significant bit (LSB) is removed.
Arithmetic (Singed) Left Shift
All digits are moved to the left except the MSB which stays the same in order to preserve the negative or positive state of the number.
The least significant bit (LSB) is padded with a zero.
Masking
Allows you to check the value of a particular bit in a binary number and is used because it is not possible to read and write those bits individually
exploits the properties of logical operators in such a way as to allow us to determine and/or change the state of one bit without affecting the state of the other bits.
AND Masking
If you wanted to find the value of the 5th bit of an 8 bit binary number (counting from the left or most significant bit) 10101010
You would use the mask 00001000
The mask uses 0s in for each bit value except the position of the bit that you want to check in the binary number.
A logical AND operation is then performed on the two numbers
A common use is IP addressing. The subnet mask extracts the network address of a device from its IP address
Network switches and bridges use this to work out if two devices are on the same network.
OR masking (setting a bit value)
Perform the logical function OR on a mask and a data bit
It is quite common for a program to have a set of flags (which are single bits) stored in a single byte or word to help keep track of events in the program.
The OR bit operation is used to set each flag when needed.
XOR masking (toggling a bit value)
Perform the logical function XOR on a mask and a data bit
This is commonly used to toggle the value of flags.