Javatpoint Logo
Javatpoint Logo

Rotate bits of a number in C++

In this article, you will learn about how to rotate bits of a number in C++.

A predetermined number of positions moves the binary representation of a number to the left or right when bits of a number are rotated. There are several bitwise manipulation tasks where this procedure can be helpful. In C++, there are two basic forms of rotation: left rotation and right rotation. These two rotations are the basis of bit rotation theory.

1. Bitwise Operators:

Bitwise Left Shift (<<): This operator moves the bits of a number to the left by a predetermined amount. The number is effectively multiplied by two to the power of the shift amount.

Bitwise Right Shift (>>): This operator moves the bits to the right in a predetermined number of positions. The number is effectively divided by 2 to the power of the shift amount. The leftmost positions of unsigned numbers are filled with zeros.

2. Left rotation: All bits are rotated to the left by a certain number of positions, and any bits that fall off the left end are wrapped around to the right end. It can be accomplished by combining the bitwise OR operation with a left shift.

3. Right rotation: It entails wrapping the bits that fall off the right end back to the left end and moving all bits a certain number of positions to the right. Bitwise OR and the appropriate shift can be used to accomplish this.

4. Data Type and Size Handling: Take the size of the data type into account while rotating the bits. There may be unpredictable behavior if bits are shifted beyond the size of the data type. To prevent problems, you should rotate bits within this size since an integer in C++ typically takes up 4 bytes (32 bits) or 8 bytes (64 bits), depending on the system.

It's critical to consider the data type's size and the possibility of overflow or underflow while utilizing bit manipulation in C++. In embedded systems, systems programming, and other scenarios requiring low-level control over data representation, bit manipulation is frequently utilized.

The bits of a number can be changed in C++ by using bitwise operations like left shift () and right shift (>>). The shifted bits are combined using the bitwise OR (|) operator to produce the rotation effect.

Example:

Output:

Original number: 64
Left rotate by 4 bits: 1024
Right rotate by 4 bits: 4

Explanation:

  1. In this example, input/output activities need the inclusion of the header file "iostream".
  2. There are two defined functions for handling left and right bit rotations, respectively: Left_Rotate and Right_Rotate. The number to be rotated and the number of bits to rotate by are the two arguments that these functions accept.
  3. With the help of the left shift operator, the Left_Rotate function rotates the bits of the number num by d degrees to the left. As a result of the bitwise OR (|) operation that follows, the bits of num is moved to the right by sizeof(num) * 8 - d positions. The left-to-right bits that are relocated ensure that they are brought back to the right end with this operation.
  4. Using the right shift operator >>, the Right_Rotate function rotates the bits of the number num by d degrees to the right. After that, the bits of num is moved left by sizeof(num) * 8 - d places as a result of a bitwise OR (|) operation. The bits that are pushed out to the right are returned to the left end due to this operation.
  5. Both an example number num_ber (which is originally set to 64) and the number of bits to rotate Rotate_By (which is initially set to 4) are defined in the main function.
  6. The original number is printed.
  7. Both the results of rotating the bits to the left by Rotate_By bits and to the right by Rotate_By bits are reported.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA