# C++ program to isolate rightmost one bit of a number

**Program to isolate rightmost one bit of a number in C++:** Here, we are going to **use bitwise operators to isolate rightmost one bit in the binary representation of a given number**.

Submitted by Saksham Bhayana, on January 07, 2019

Here we are going to use bitwise operators to **isolate rightmost one bit in the binary representation of a given number**.

**Problem Statement:** To write a C++ program to isolate rightmost one bit of a number.

**Constraints: 1<=n<=100**

**Example:**

Input: Enter number: 18 Output: original number before isolating rightmost 1 bit: 18 new number after isolating rightmost 1 bit: 2

**Problem Explanation:**

Suppose the given number is 18. Let’s denote rightmost one bit by ROB.

Now the binary representation of 18 is:

n = 00010010 mask = -n = 11101110 (toggle all bits and 1 to it) Binary representation of number after isolating ROB = n & mask n & mask = 00000010 Hence decimal representation of new number is 2.

**This can be explained as follows:**

When we create a mask of -n, we observe that only ROB and bits to the right of ROB (which are 0) are identical to the original number.

Therefore, to isolate the ROB we can do bitwise AND of the mask with n as only the ROB is 1 in both the masks.

**Algorithm:**

- Input the number for which ROB is to be isolated.
- Create a mask -n (first toggle all bits in n and then add 1 to it).
- Perform bitwise AND of the mask created with original number i.e. n & mask where mask is of -n.
- Output the result after bitwise AND in decimal form.

**C++ Implementation:**

#include <iostream> using namespace std; int isolate_rightmost_onebit(int n) { //to compute -n, toggle all bits and add 1 to it int mask=-n; // new number after isolating rightmost 1 bit return (n&mask); } //driver program to check the code int main() { int num; cout<<"Enter number: "; cin>>num; cout<<"original number before isolating rightmost 1 bit: "<<num<<endl; int new_number= isolate_rightmost_onebit(num); cout<<"new number after isolating rightmost 1 bit: "<<new_number<<endl; return 0; }

**Output**

Enter number: 18 original number before isolating rightmost 1 bit: 18 new number after isolating rightmost 1 bit: 2

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.