diff --git a/Bit Manipulation/Bit Reversal/ReadMe.md b/Bit Manipulation/Bit Reversal/ReadMe.md new file mode 100644 index 00000000..b6a7b507 --- /dev/null +++ b/Bit Manipulation/Bit Reversal/ReadMe.md @@ -0,0 +1,45 @@ +# Bit Reversal Using Bit Manipulation + +This project demonstrates how to reverse the bits of a 32-bit unsigned integer using bit manipulation techniques in C. The algorithm efficiently reverses the bits by iterating through each bit position and setting the corresponding bit in the reversed position. + +## Features + +- Reverses the bits of a 32-bit unsigned integer. +- Utilizes bit manipulation techniques for efficient processing. + +## Algorithm + +The bit reversal algorithm involves the following steps: + +1. **Initialize** a variable to store the reversed bits. +2. **Calculate** the total number of bits in the integer (32 bits for this implementation). +3. **Iterate** through each bit position of the integer: + - Check if the current bit is set. + - If the bit is set, set the corresponding bit in the reversed variable. +4. Return the reversed integer. + +## Sample Input and Output + +1. **Input**: `1` + - **Output**: `2147483648` + +2. **Input**: `2` + - **Output**: `1073741824` + +3. **Input**: `43261596` + - **Output**: `964176192` + +4. **Input**: `4294967295` + - **Output**: `4294967295` + +5. **Input**: `0` + - **Output**: `0` + +## How to Compile and Run + +1. **Save the Code**: Save the provided C code in a file named `bit_reversal.c`. +2. **Open a Terminal**: Use your terminal application on macOS or Linux. +3. **Navigate to the Directory**: Use the `cd` command to navigate to the directory where `bit_reversal.c` is saved. +4. **Compile the Code**: Use the following command to compile the code: + ```sh + gcc -o bit_reversal bit_reversal.c diff --git a/Bit Manipulation/Bit Reversal/program.c b/Bit Manipulation/Bit Reversal/program.c new file mode 100644 index 00000000..98dc425a --- /dev/null +++ b/Bit Manipulation/Bit Reversal/program.c @@ -0,0 +1,41 @@ +#include + +// Function to reverse the bits of a given integer +unsigned int reverseBits(unsigned int n) { + unsigned int reversed = 0; // Variable to store the reversed bits + unsigned int bitCount = sizeof(n) * 8; // Total number of bits in the integer + + // Iterate through each bit of the integer + for (unsigned int i = 0; i < bitCount; i++) { + // Check if the i-th bit in n is set + if ((n & (1 << i))) { + // Set the corresponding bit in the reversed variable + reversed |= 1 << ((bitCount - 1) - i); + } + } + return reversed; +} + +// Function to test the reverseBits function +void testReverseBits() { + unsigned int num; // Variable to store the input number + + // Prompt the user to enter a number + printf("Enter a number: "); + scanf("%u", &num); // Read the number from standard input + + // Call the reverseBits function to reverse the bits of the number + unsigned int reversedNum = reverseBits(num); + + // Display the original and reversed numbers + printf("Original number: %u\n", num); + printf("Reversed number: %u\n", reversedNum); +} + +// Main function +int main() { + // Call the test function to demonstrate the bit reversal + testReverseBits(); + + return 0; // Successful completion of the program +}