Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print steps to make a number in form of 2^X – 1 in C Program.
Given a number n, we have to print the steps to make the number in the form of 2^X-1 by using XOR operation and increment operations.
- At odd steps, XOR the number with any 2^M-1, where M is the position of the leftmost unset bit.
- At even steps, increment the number by 1.
Keep performing these steps until n becomes 2^X-1 (a number with all bits set), and print all the steps.
Syntax
int find_leftmost_unsetbit(int n); void perform_steps(int n);
Algorithm
The algorithm works by finding the leftmost unset bit and alternating between XOR operations and increment operations −
- Find the leftmost unset bit position
- If no unset bit exists, the number is already in 2^X-1 form
- Alternate between XOR with 2^M-1 (odd steps) and increment by 1 (even steps)
- Continue until all bits are set
Example
Here's a complete implementation that demonstrates the step-by-step conversion −
#include <stdio.h>
#include <math.h>
// Function to find the leftmost unset bit position
int find_leftmost_unsetbit(int n) {
int ind = -1;
int i = 1;
while (n) {
if (!(n & 1))
ind = i;
i++;
n >>= 1;
}
return ind;
}
// Function to perform the steps to convert number to 2^X-1 form
void perform_steps(int n) {
printf("Converting %d to 2^X-1 form:<br>", n);
// Find the leftmost unset bit
int left = find_leftmost_unsetbit(n);
// If there is no unset bit, number is already in 2^X-1 form
if (left == -1) {
printf("No Steps to be performed<br>");
return;
}
// To count the number of steps
int step = 1;
// Iterate till number is in form of 2^X - 1
while (find_leftmost_unsetbit(n) != -1) {
// If the step is even then increase by 1
if (step % 2 == 0) {
n += 1;
printf("Step %d: Increase by 1<br>", step);
}
// If the step is odd then XOR with 2^M-1
else {
// Finding the leftmost unset bit
int m = find_leftmost_unsetbit(n);
int num = (int)(pow(2, m) - 1);
n = n ^ num;
printf("Step %d: Xor with %d<br>", step, num);
}
// Increment the step counter
step += 1;
}
printf("Final result: %d (which is 2^X-1 form)<br>", n);
}
int main() {
int n1 = 22;
perform_steps(n1);
printf("<br>");
int n2 = 7;
perform_steps(n2);
return 0;
}
Converting 22 to 2^X-1 form: Step 1: Xor with 15 Step 2: Increase by 1 Step 3: Xor with 7 Step 4: Increase by 1 Step 5: Xor with 1 Final result: 31 (which is 2^X-1 form) Converting 7 to 2^X-1 form: No Steps to be performed
How It Works
For input 22 (binary: 10110):
- Step 1: Leftmost unset bit is at position 4, so XOR with 15 (2^4-1)
- Step 2: Increment by 1 (even step)
- Step 3: XOR with 7 (2^3-1) based on new leftmost unset bit
- Step 4: Increment by 1
- Step 5: XOR with 1 (2^1-1)
- Result: 31 (binary: 11111) which is 2^5-1
Conclusion
This algorithm efficiently converts any number to 2^X-1 form by alternating between XOR operations with powers of 2 minus 1 and simple increments. The process continues until all bits in the number are set to 1.
Advertisements
