
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to rotate the bits for a given number
Consider the factors given below to write a C program to rotate the bits for a given number.
Rotating the bit from left to right or right to left.
In left rotation, the bits are shifted from left to right.
In right rotation, the bits are shifted from right to left.
Take a number and try to rotate either left or right based on user program.
User has to enter the number rotation at run time along with a number.
Program 1
Following is the C program to apply left rotation for a given number.
#include<stdio.h> #include<stdlib.h> int main(){ int number, rotate, Msb, size; printf("Enter any number:"); scanf("%d",&number); printf("Enter number of rotations:
"); scanf("%d",&rotate); size = sizeof(int) * 8; rotate %= size; while(rotate--){ Msb = (number >> size) & 1; number = (number << 1) | Msb; } printf("After Left rotation the value is = %d
",number); return 0; }
Output
When the above program is executed, it produces the following result −
Enter any number:12 Enter number of rotations: 2 After Left rotation the value is = 48
Program 2
Given below is the C program to apply right rotation for a given number.
#include<stdio.h> #include<stdlib.h> int main(){ int number,rotate, Lsb, size; printf("Enter any number:"); scanf("%d",&number); printf("Enter number of rotations:
"); scanf("%d",&rotate); size = sizeof(int) * 8; rotate %= size; while(rotate--){ Lsb = number & 1; number = (number >> 1) &(~(1<<size)); number=number|(Lsb<<size); } printf("After right rotation the value is = %d
",number); return 0; }
Output
When the above program is executed, it produces the following result −
Enter any number:18 Enter number of rotations: 2 After right rotation the value is = 4
- Related Articles
- Haskell Program to get total Bits Required for the Given Number using Library Function
- Golang Program to get total bits required for the given number using library function
- C# program to count total bits in a number
- Minimum number using set bits of a given number in C++
- C# program to count total set bits in a number
- Program to invert bits of a number Efficiently in C++
- C program to find Fibonacci series for a given number
- C Program to find two’s complement for a given number
- Write an Efficient C Program to Reverse Bits of a Number in C++
- Java program to count total bits in a number
- Java program to extract ‘k’ bits from a given position
- Python program to extract ‘k’ bits from a given position?
- Count number of bits changed after adding 1 to given N in C++
- Python program to reverse bits of a positive integer number?
- Java program to reverse bits of a positive integer number

Advertisements