Write a program to reverse the bits of a given number using for loop
C program to reverse the bits of a given number using for loop
This program in the C programming language is designed to reverse the order of the bits in a given number. The program uses a for loop to iterate through each bit in the number and swap its position with the corresponding bit on the opposite end of the number.
#include<stdio.h>
int main()
{
int num,pos,i,j,m,n;
printf("Enter any number:");
scanf("%d",&num);
printf("Before reversing the number:\n");
for(pos=31;pos>=0;pos--)
{
printf("%d",num>>pos&1);
}
printf("\n");
for(i=0,j=31;i<j;i++,j--)
{
m=num>>i&1;
n=num>>j&1;
if(m!=n)
{
num=num^1<<i;
num=num^1<<j;
}
}
printf("After reversing the number:\n");
for(pos=31;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
Enter any number: 10
Before reversing the number:
00000000000000000000000000001010
After reversing the number:
01010000000000000000000000000000
C program that reverses the bits of an integer number. Here's what the program does:
- The program prompts the user to enter an integer number.
- The program uses a for loop to print the binary representation of the number before it is reversed.
- The program uses two variables i and j to traverse the binary representation of the number from left to right and from right to left respectively. Inside the loop, the program uses bitwise operations to extract the bit values at position i and j and checks if they are different.
- If the bit values at positions i and j are different, the program uses bitwise
XOR
operations to swap the values of the two bits by flipping them. This is done using bitwise shift operations to create a mask with a 1 in the appropriate bit position and then using bitwiseXOR
with the original number to flip the bit. - The program uses another for loop to print the binary representation of the number after it has been reversed.
Note that this program assumes that the integer number entered by the user is a 32-bit integer
since it uses a for loop to traverse the binary representation of the number from position 31 (most significant bit)
to position 0 (least significant bit).
Program 02: C program to reverse the bits of a given number using for loop with function
#include<stdio.h>
unsigned int reverse_bits(unsigned int num) {
unsigned int count = sizeof(num) * 8 - 1;
unsigned int reverse_num = num;
num >>= 1;
while (num) {
reverse_num <<= 1;
reverse_num |= num & 1;
num >>= 1;
count--;
}
reverse_num <<= count;
return reverse_num;
}
int main() {
unsigned int num;
printf("Enter a number: ");
scanf("%u", &num);
printf("Reversed bits of %u is %u\n", num, reverse_bits(num));
return 0;
}
Reverses the bits of an unsigned integer number. Here's what the program does:
- The program defines a function
reverse_bits
that takes an unsigned integer number as input and returns the reversed bits of that number. - Inside the function, the program calculates the number of bits in the input number by subtracting
1
from the number of bits in the integer type(unsigned int)
and assigns the value to the variablecount.
- The program initializes a new variable
reverse_num
with the input number. - The program then shifts the input number to the right by
1-bit
position and enters a while loop that runs as long as the input number isnon-zero.
- Inside the loop, the program shifts the
reverse_num
variable to the left by 1 bit position and uses a bitwise OR operation to set the least significant bit of reverse_num to the value of the least significant bit of the input number(num & 1).
- The program shifts the input number to the right by 1 bit position and decrements the
count
variable. - Once the loop is complete, the program shifts the
reverse_num
variable to the left by count bit positions to pad the remaining bits with zeros. - The program returns the
reverse_num
variable. - In the
main
function, the program prompts the user to enter an unsigned integer number, reads the input from the console using thescanf
function, and calls thereverse_bits
function to reverse the bits of the input number. - The program then prints the reversed bits of the input number to the console using the
printf
function.
Note that this implementation assumes the input number is an unsigned integer. If you want to reverse the bits of a signed integer, you'll need to handle the sign bit separately
Top Resources
Copy in a reverse manner in c
Write a program to reverse printing not reversing of 10 elements of array in c
Write a program to reverse the bits of a given number using for loop
Design a function to reverse the given string
Design a function to reverse the word in given string
write a program to copy one array element to another array element in reverse manner.
write a program to reverse adjacent elements of an array.
write a program to reverse half of the elements of an array.
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!