-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1135 from KAUSHIKRM-36/feature/BitReversal
Add implementation of Bit Reversal using Bit Manipulation
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
|
||
// 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 | ||
} |