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

Program to isolate rightmost zero bit of a number in C++: Here, we are going to use bitwise operators to isolate rightmost zero 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 zero bit in the binary representation of a given number.

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

Constraints: 1<=n<=100

Example:

```    Input:
Enter number: 11

Output:
original number before isolating rightmost 0 bit: 11
new number after isolating rightmost 0 bit: 4
```

Problem Explanation:

Suppose the given number is 11. Let’s denote rightmost zero bit by RZB.

Now the binary representation of 11 is:

```    n = 00001011
mask2 = 11110100 (bitwise complement of n)
Hence decimal representation of new number is 4.
```

This can be explained as follows:

When we add 1 to n the RZB is changed to 1 and all the bits to the right of RZB becomes zero and all the bits to the left are unchanged.

When we do bitwise complement of n, the RZB is changed to 1. The bits to the right of RZB become 0 and bits to the left are toggled.

Therefore to isolate the RZB we can do bitwise AND of the two masks as only the RZBis 1 in both the masks.

Algorithm:

1. Input the number for which RZB is to be isolated.
3. Create a mask2 by performing bitwise complement of original number (toggle all bits).
5. Output the result after bitwise AND in decimal form.

C++ Implementation:

```#include <iostream>
using namespace std;

int isolate_rightmost_zerobit(int n)
{
int mask2=~(n);         // bitwise complement of original number
}

//driver program to check the code

int main()
{
int num;

cout<<"Enter number: ";
cin>>num;
cout<<"original number before isolating rightmost 0 bit: "<<num<<endl;

int new_number= isolate_rightmost_zerobit(num);

cout<<"new number after isolating rightmost 0 bit: "<<new_number<<endl;

return 0;
}
```

Output

```Enter number: 11
original number before isolating rightmost 0 bit: 11
new number after isolating rightmost 0 bit: 4
```

TOP Interview Coding Problems/Challenges

Learn PCB Designing: PCB DESIGNING TUTORIAL

 Recommended posts C Tips & Tricks, C++ Tips & Tricks Introduction to Linux (Its modes, Safety, Most popular Applications) Linux Best Distros of 2018 C programming optimization techniques Differences b/w C & Embedded C? Embedded C Interview Q. & A. C programming tips for Embedded Development Basic rules of writing a C program Important points (rules) to remember while writing C/C++ program Top 5 Websites for solving programming challenges Read more...

 Others... Computer G.K. (MCQ) Most viewed pages... Categories...

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates