Write a C program to input a number and rotate bits of number using bitwise shift operators. How to rotate bits of a given number using bitwise operator in C programming. Logic to left or right rotate bits of a number using bitwise shift operator in C program.
Example
Input
Input number = -15 Number of rotations = 2
Output
-15 left rotated 2 times = -57 -15 right rotated 2 times = 2147483644
Required knowledge
Basic C programming, Bitwise operators, Functions
What is meant by rotating bits of a number
Rotating bits of a number to left, means shifting all bits to left and pushing the dropped Most Significant Bit to Least Significant Bit.
Rotating bits of a number to right, means shifting all bits to right and pushing the dropped Least Significant Bit to Most Significant Bit.

There is small difference in logic of left and right rotation of number. I have explained logic of both problems separately.
Logic to left rotate bits in a number
Left rotation of bits in C is supported using bitwise left shift operator <<
. But left shift operator drops Most Significant Bit (MSB) on each shift. Which is what we don’t want. Instead of dropping MSB on each rotation, Least Significant Bit (LSB) should get replaced as dropped MSB.
Step by step descriptive logic to left rotate bits of a number.
- Input number to rotate and rotation amount from user. Store it in some variable say
num
androtation
. - Find most significant bit of the number that is about to drop and store it in some variable say
DROPPED_MSB = (num >> INT_BITS) & 1
. - Left Shift
num
one times and set its least significant bit toDROPPED_MSB
i.e. performnum = (num << 1) | DROPPED_MSB
. - Repeat step 2 and 3 for given number of rotations.
Logic to right rotate bits of a number
Right rotation of bits in C programming is supported using bitwise right shift operator >>
. Similar to left shift, right shift operations also results in bit loss. On every shift operation the least significant bit is dropped.
What we need to do is, instead of dropping the least significant bit replace most significant bit with the dropped least significant bit.
Step by step descriptive logic to right rotate bits of a number.
- Input number to rotate and rotation amount from user. Store it in some variable say
num
androtation
. - Find least significant bit of the number about to drop and store it in some variable say
DROPPED_LSB = num & 1
. - Right shift number one time i.e. perform
num = num >> 1
. - Clear most significant bit of number i.e. perform
num = num & (~(1 << INT_BITS))
. - Set the
DROPPED_LSB
as most significant bit ofnum
. Performnum = num | (DROPPED_LSB << INT_BITS)
. - Repeat step 2 to 5 for given number of rotations.
Note:
INT_BITS
is total number of bits in integer – 1.
Program to rotate bits of a number
Note: I have used
%u
format specifier forunsigned int
. Learn about various format specifiers used in C.
To short the logic you can combine all bitwise operations as one. You can further decay above two functions as.
Output
Enter a number: -15 Enter number of rotation: 2 -15 left rotated 2 times = -57 -15 right rotated 2 times = 2147483644
Recommended posts
- Bitwise operator programming exercises index.
- C program to flip bits of a binary number using bitwise operator.
- C program to count zeros and ones in a binary number.
- C program to get highest set bit of a number.
- C program to count leading zeros in a binary number.
- C program to toggle nth bit of a number.