Bit Manipulation 1 | Binary Shifts | Monitoring and Control |A/AS Level | By ZAK

Поділитися
Вставка
  • Опубліковано 21 гру 2023
  • Show understanding of and perform binary shifts:
    Logical, arithmetic and cyclic
    Left shift, right shift
    Show understanding of how bit manipulation can be used to monitor/control a device:
    Carry out bit manipulation operations
    Test and set a bit (using bit masking)
    Understanding Binary Shifts
    Binary shifts are operations that move the bits in a binary number to the left or right, with different types of shifts having different effects on the number.
    1. Logical Shift:
    - Left Logical Shift: Moves all bits in a binary number to the left by a specified number of positions. Zeros are shifted in from the right. The value of the number effectively gets multiplied by 2 for each shift left.
    - Right Logical Shift: Moves all bits to the right. Zeros are shifted in from the left. This shift effectively divides the number by 2 for each shift right, disregarding any remainders.
    2. Arithmetic Shift:
    - Left Arithmetic Shift: Similar to the left logical shift and has the same effect of multiplying the number by 2 for each shift.
    - Right Arithmetic Shift: Moves all bits to the right while preserving the sign bit (the leftmost bit in a signed number). This shift is used with signed numbers to maintain their sign while effectively performing a division by 2.
    3. Cyclic (or Circular) Shift:
    - Left/Right Cyclic Shift: Moves the bits around in a circle. A left cyclic shift moves all bits to the left, and the leftmost bit is wrapped around to the rightmost position. A right cyclic shift does the reverse.
    Bit Manipulation for Device Control
    Bit manipulation is a powerful tool in programming, especially for controlling devices, as it allows for efficient manipulation of individual bits.
    1. Carry Out Bit Manipulation Operations:
    - Setting a Bit: To set (turn to 1) a specific bit, use the OR operator (`|`) with a bitmask that has a 1 at the position of the bit to be set.
    - Clearing a Bit: To clear (turn to 0) a specific bit, use the AND operator (`&`) with a bitmask that has a 0 at the position of the bit to be cleared.
    - Toggling a Bit: To toggle (invert) a specific bit, use the XOR operator (`^`) with a bitmask that has a 1 at the position of the bit to be toggled.
    - Checking a Bit: To check the value of a specific bit, use the AND operator with a bitmask that has a 1 at the position of the bit to be checked. If the result is non-zero, the bit is set.
    2. Test and Set a Bit (Using Bit Masking):
    - To test and set a bit, you first check if a bit is 0 or 1. If it's 0, you set it to 1. This is often done in a single operation for efficiency, especially in multi-threaded environments where atomicity of such operations can be critical.
    Let's demonstrate these operations with examples in Python: setting a bit, clearing a bit, toggling a bit, and testing a bit. We'll use an 8-bit binary number for simplicity.
    Here are the results of the bit manipulation operations on the binary number `10101010` (in decimal, 170):
    1. Setting the 3rd Bit: The result is `10101110` (in decimal, 174). The 3rd bit (counting from 0) was set to 1.
    2. Clearing the 5th Bit: The result is `10101010` (in decimal, 170). The 5th bit was cleared to 0.
    3. Toggling the 6th Bit: The result is `10001010` (in decimal, 138). The 6th bit was toggled (changed from 1 to 0).
    4. Testing the 2nd Bit: The result is `True`, indicating that the 2nd bit is indeed set to 1 in the original number.
    These operations demonstrate how specific bits within a byte can be manipulated - a common requirement in systems programming, especially when interfacing with hardware, where each bit in a control register might enable, disable, or configure a different component of a device.

КОМЕНТАРІ •