In this blog post, I will teach you how to write a C/C++ program to rotate bits of a number. After reading this post you can write an efficient program to rotate bits of a number.
There are two types of rotation left rotation and second one is right rotation. In the left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put back at left end.
Let’s consider the number 27.
Here’s a simple ASCII representation of 27 (an 8-bit integer):
0001 1011
Example 1: Now let’s shift the number in left Rotation by 1 position:
7 6 5 4 3 2 1 0 (Bit position) +---+---+---+---+---+---+---+---+ | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | (Example bit values) +---+---+---+---+---+---+---+---+ --- Now left rotate by 1 position ---- 7 6 5 4 3 2 1 0 (Bit position) +---+---+---+---+---+---+---+---+ | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | (Example bit values) +---+---+---+---+---+---+---+---+
Example 2: Now let’s shift the number in left Rotation by 1 position:
Original number: 7 6 5 4 3 2 1 0 (Bit position) +---+---+---+---+---+---+---+---+ | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | (Example bit values) +---+---+---+---+---+---+---+---+ --- Now Right rotate by 1 position ---- 7 6 5 4 3 2 1 0 (Bit position) +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | (Example bit values) +---+---+---+---+---+---+---+---+
Let’s see the problem statements for this question.
Problem Statement:
Given an integer n, rotate bits of a n.
Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.
C program to Rotate bits of a number:
/* C program to Rotate bits of a given integer and returns the result */ #include<stdio.h> #define INT_BITS 32 //C Function to left rotate n by t bits int leftRotate(int n, unsigned int t) { /* In n<< t, last t bits become 0. To put first t bits of n at last, Need to do bitwise or of n<<t with n >>(INT_BITS - t) */ return (n << t)|(n >> (INT_BITS - t)); } //C Function to right rotate n by t bits int rightRotate(int n, unsigned int t) { /* In n<< t, first t bits become 0. To put last t bits of n at first, Need to do bitwise or of n>>t with n <<(INT_BITS - t) */ return (n >> t)|(n << (INT_BITS - t)); } int main() { int n = 28; int t = 2; printf("Left Rotation of %d by %d is ", n, t); const unsigned int leftRotateN = leftRotate(n, t); printf("%d", leftRotateN); printf("\nRight Rotation of %d by %d is ", n, t); const unsigned int rightRotateN = rightRotate(n, t); printf("%d", rightRotateN); return 0; }
Output:
Left Rotation of 28 by 2 is 112
Right Rotation of 28 by 2 is 7
C++ program to Rotate bits of a number:
/* C++ program to Rotate bits of a given integer and returns the result */ #include <iostream> #define INT_BITS 32 //C Function to left rotate n by t bits int leftRotate(int n, unsigned int t) { /* In n<< t, last t bits become 0. To put first t bits of n at last, Need to do bitwise or of n<<t with n >>(INT_BITS - t) */ return (n << t)|(n >> (INT_BITS - t)); } //C Function to right rotate n by t bits int rightRotate(int n, unsigned int t) { /* In n<< t, first t bits become 0. To put last t bits of n at first, Need to do bitwise or of n>>t with n <<(INT_BITS - t) */ return (n >> t)|(n << (INT_BITS - t)); } int main() { int n = 28; int t = 2; std::cout<<"Left Rotation n by "<<t<<std::endl; const unsigned int leftRotateN = leftRotate(n, t); std::cout<< leftRotateN <<std::endl; std::cout<<"Right Rotation n by "<<t<<std::endl; const unsigned int rightRotateN = rightRotate(n, t); std::cout<< rightRotateN <<std::endl; return 0; }
Recommended Post
- Count Set bits in an Integer.
- Count Set bits in an Integer using the lookup table.
- How to set, clear or toggle a single bit in C/C++?
- Macros for Bit Manipulation in C/C++.
- 5 Ways to Check if Two Integers have Opposite Signs.
- C Program to Find whether a given integer is a power of three or not.
- Operator Precedence And Associativity In C.
- Operators in C language