Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

4.4 KB
Raw Blame

Shifts and rotates are operations commonly used in computer arithmetic and data manipulation. They manipulate binary data at the bit level, and they can be performed on both signed and unsigned integers. Here’s an overview of both operations, including how they work, types, and examples.

1. Shifts

Shifts are operations that move bits in a binary number to the left or right. Shifting can be categorized into two main types: logical shifts and arithmetic shifts.

a. Logical Shift

  • Logical Left Shift (<<): Moves bits to the left, filling the vacated bits with 0s. The leftmost bit (MSB) is discarded.

    Example:

    • Binary: 0010 1010 (decimal 42)
    • Logical Left Shift: 0101 0100 (decimal 84)
    • The operation: 0010 1010 << 1 = 0101 0100
  • Logical Right Shift (>>): Moves bits to the right, filling the vacated bits with 0s. The rightmost bit (LSB) is discarded.

    Example:

    • Binary: 0010 1010 (decimal 42)
    • Logical Right Shift: 0001 0101 (decimal 21)
    • The operation: 0010 1010 >> 1 = 0001 0101

b. Arithmetic Shift

  • Arithmetic Left Shift (<<): Similar to logical left shift; it moves bits to the left and fills with 0s.

  • Arithmetic Right Shift (>>): Moves bits to the right while preserving the sign bit (the MSB). This means if the number is negative (MSB is 1), the vacated bits are filled with 1s; if positive, they are filled with 0s.

    Example:

    • Binary: 1111 0100 (decimal -12 in two's complement)
    • Arithmetic Right Shift: 1111 1010 (decimal -6)
    • The operation: 1111 0100 >> 1 = 1111 1010

2. Rotates

Rotates are operations that shift bits circularly around a defined boundary, meaning that bits shifted out on one end are reintroduced on the opposite end.

a. Rotate Left (ROL)

  • Moves bits to the left, and the leftmost bit that is shifted out is brought back to the rightmost position.

    Example:

    • Binary: 0010 1010
    • Rotate Left: 0101 0100
    • The operation: 0010 1010 ROL 1 = 0101 0100

b. Rotate Right (ROR)

  • Moves bits to the right, and the rightmost bit that is shifted out is brought back to the leftmost position.

    Example:

    • Binary: 0010 1010
    • Rotate Right: 0001 0101
    • The operation: 0010 1010 ROR 1 = 1000 1010

Summary Table

Operation Direction Description Example
Logical Left Shift (<<) Left Moves bits left, fills with 0s 0010 1010 << 1 = 0101 0100
Logical Right Shift (>>) Right Moves bits right, fills with 0s 0010 1010 >> 1 = 0001 0101
Arithmetic Left Shift Left Same as logical left shift 0010 1010 << 1 = 0101 0100
Arithmetic Right Shift Right Moves bits right, fills with sign bit 1111 0100 >> 1 = 1111 1010 (for -12)
Rotate Left (ROL) Left Moves bits left, wraps around the leftmost bit 0010 1010 ROL 1 = 0101 0100
Rotate Right (ROR) Right Moves bits right, wraps around the rightmost bit 0010 1010 ROR 1 = 0001 0101

Applications

  1. Multiplication and Division: Shifting left can effectively multiply a binary number by 2, while shifting right can divide it by 2. This is especially useful in low-level programming and embedded systems where performance is critical.

  2. Data Manipulation: Shifts and rotates are used in graphics processing, encryption algorithms, and checksum calculations.

  3. Circular Buffers: Rotate operations are utilized in circular buffer implementations, where data is read and written in a wrap-around manner.

  4. Cryptography: Rotating bits can enhance security in cryptographic algorithms.

Conclusion

Shifts and rotates are essential operations in binary arithmetic and data manipulation, serving crucial roles in performance optimization and low-level programming. Understanding these operations allows programmers and engineers to manipulate binary data efficiently and effectively.