Understanding Bit Shift Operations
Bit shift operations are used to move the bits of a binary number to the left or right, which effectively multiplies or divides the number by powers of two.
Operations:
- Left Shift (`<<`): Moves bits to the left by the specified amount. Adds zeros on the right. This operation multiplies the number by 2 for each shift. Example: `5 << 1` is `10` (binary: `0101 << 1 = 1010`).
- Right Shift (`>>`): Moves bits to the right by the specified amount. Discards bits shifted off. This operation divides the number by 2 for each shift (ignoring remainders). Example: `5 >> 1` is `2` (binary: `0101 >> 1 = 0010`).
Example Calculations
-
Left Shift Example:
- Input Integer: `8` (binary: `1000`)
- Shift Amount: `2`
- Operation: `8 << 2`
- Binary After Shift: `100000`
- Result: `32`
-
Right Shift Example:
- Input Integer: `36` (binary: `100100`)
- Shift Amount: `2`
- Operation: `36 >> 2`
- Binary After Shift: `1001`
- Result: `9`
Use the calculator above to experiment with different bit shift operations and see how they work!