
- 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
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 as in form of 2^X-1 by using Xor operation.
- We should XOR the number with any 2^M-1, where M is chosen by you, at odd step.
- At even step increment the number by 1
Keep performing the step until n becomes 2^X-1, and print all the steps
Example
Input: 22 Output: 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 Input:7 Output: No Steps to be performed
Algorithm
int find_leftmost_unsetbit(int n) START STEP 1 : DECLARE AND ASSIGN ind = -1, i = 1 STEP 2 : LOOP WHILE n IF !(n & 1) THEN, ASSIGN ind WITH i END IF INCREMENT i BY 1 LEFT SHIFT n BY 1 END WHILe STEP 3 : RETURN ind STOP void perform_steps(int n) START STEP 1 : DECLARE AND ASSIGN left = find_leftmost_unsetbit(n) STEP 2 : IF left == -1 THEN, PRINT "No Steps to be performed" RETURN END IF STEP 3 : DECLARE AND ASSIGN step = 1 STEP 4 : LOOP WHILE find_leftmost_unsetbit(n) != -1 IF step % 2 == 0 THEN, INCREMENT n BY 1 PRINT "Step n : Increase by 1
" ELSE DECLARE AND ASSIGN m = find_leftmost_unsetbit(n) AND SET num = (pow(2, m) - 1) SET n = n ^ num PRINT "Step N : Xor with Num END IF INCREMENT step BY 1 END LOOP STOP
Example
#include <stdio.h> #include <math.h> //To find the leftmost bit int find_leftmost_unsetbit(int n){ int ind = -1; int i = 1; while (n) { if (!(n & 1)) ind = i; i++; n >>= 1; } return ind; } void perform_steps(int n){ // Find the leftmost unset bit int left = find_leftmost_unsetbit(n); //If there is no bit if (left == -1) { printf("No Steps to be performed
"); 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
", 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
", step, num); } // To increase the steps step += 1; } } int main(){ int n = 22; perform_steps(n); return 0; }
Output
If we run above program then it will generate following output −
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
- Related Articles
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Program to print maximum number of characters by copy pasting in n steps in Python?
- Program to find longest consecutive run of 1 in binary form of a number in C++
- C++ Program to Print Matrix in Z form?
- Counting steps to make number palindrome in JavaScript
- Program to print number pattern in C
- C++ program to count number of stairways and number of steps in each stairways
- C++ program to count number of steps needed to make sum and the product different from zero
- C++ program to count minimum number of operations needed to make number n to 1
- Goland Program to Read a Number (n) And Print the Series "1+2+…..+n= "
- Python Program to Read a Number n And Print the Series "1+2+…..+n= "
- Print the arranged positions of characters to make palindrome in C Program.
- Program to Print the Squared Matrix in Z form in C
- C++ Program to Print the Multiplication Table in Triangular Form

Advertisements