Given a positive integer ‘n’ having ‘x’ number of set bits in its binary representation. The problem is to find the next greater integer(smallest integer greater than n), having (x+1) number of set bits in its binary representation.

Examples :

Input : 10 Output : 11(10)= (1010)_{10}_{2}is having 2 set bits.(11)= (1011)_{10}_{2}is having 3 set bits and is the next greater. Input : 39 Output : 47

**Approach:** Following are the steps:

- Find the position of the rightmost unset bit(considering last bit at position 0, second last bit at position 1 and so on) in the binary representation of
**n**. - Let the position be represented by
**pos**. - Set the bit at position
**pos**. Refer this post. - If there are no unset bits in the binary representation, then perform bitwise left shift by 1 on the given number and then add 1 to it.

**How to get the position of rightmost unset bit?**

- Perform bitwise not on the given number(operation equivalent to 1’s complement).Let it be
**num**= ~n. - Get the position of rightmost set bit of
**num**.

## C++

`// C++ implementation to find the next greater integer ` `// with one more number of set bits ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to find the position of rightmost ` `// set bit. Returns -1 if there are no set bits ` `int` `getFirstSetBitPos(` `int` `n) ` `{ ` ` ` `return` `(log2(n&-n)+1) - 1; ` `} ` ` ` `// function to find the next greater integer ` `int` `nextGreaterWithOneMoreSetBit(` `int` `n) ` `{ ` ` ` `// position of rightmost unset bit of n ` ` ` `// by passing ~n as argument ` ` ` `int` `pos = getFirstSetBitPos(~n); ` ` ` ` ` `// if n consists of unset bits, then ` ` ` `// set the rightmost unset bit ` ` ` `if` `(pos > -1) ` ` ` `return` `(1 << pos) | n; ` ` ` ` ` `//n does not consists of unset bits ` ` ` `return` `((n << 1) + 1); ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << ` `"Next greater integer = "` ` ` `<< nextGreaterWithOneMoreSetBit(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the next greater integer ` `// with one more number of set bits ` `class` `GFG { ` ` ` ` ` `// function to find the position of rightmost ` ` ` `// set bit. Returns -1 if there are no set bits ` ` ` `static` `int` `getFirstSetBitPos(` `int` `n) ` ` ` `{ ` ` ` `return` `((` `int` `)(Math.log(n & -n) / Math.log(` `2` `)) + ` `1` `) - ` `1` `; ` ` ` `} ` ` ` ` ` `// function to find the next greater integer ` ` ` `static` `int` `nextGreaterWithOneMoreSetBit(` `int` `n) ` ` ` `{ ` ` ` ` ` `// position of rightmost unset bit of n ` ` ` `// by passing ~n as argument ` ` ` `int` `pos = getFirstSetBitPos(~n); ` ` ` ` ` `// if n consists of unset bits, then ` ` ` `// set the rightmost unset bit ` ` ` `if` `(pos > -` `1` `) ` ` ` `return` `(` `1` `<< pos) | n; ` ` ` ` ` `// n does not consists of unset bits ` ` ` `return` `((n << ` `1` `) + ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `System.out.print(` `"Next greater integer = "` ` ` `+ nextGreaterWithOneMoreSetBit(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# the next greater integer with ` `# one more number of set bits ` `import` `math ` ` ` `# Function to find the position ` `# of rightmost set bit. Returns -1 ` `# if there are no set bits ` `def` `getFirstSetBitPos(n): ` ` ` ` ` `return` `((` `int` `)(math.log(n & ` `-` `n) ` `/` ` ` `math.log(` `2` `)) ` `+` `1` `) ` `-` `1` ` ` `# Function to find the next greater integer ` `def` `nextGreaterWithOneMoreSetBit(n): ` ` ` ` ` `# position of rightmost unset bit of ` ` ` `# n by passing ~n as argument ` ` ` `pos ` `=` `getFirstSetBitPos(~n) ` ` ` ` ` `# if n consists of unset bits, then ` ` ` `# set the rightmost unset bit ` ` ` `if` `(pos > ` `-` `1` `): ` ` ` `return` `(` `1` `<< pos) | n ` ` ` ` ` `# n does not consists of unset bits ` ` ` `return` `((n << ` `1` `) ` `+` `1` `) ` ` ` `# Driver code ` `n ` `=` `10` `print` `(` `"Next greater integer = "` `, ` ` ` `nextGreaterWithOneMoreSetBit(n)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the next greater ` `// integer with one more number of set bits ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find the position of rightmost ` ` ` `// set bit. Returns -1 if there are no set bits ` ` ` `static` `int` `getFirstSetBitPos(` `int` `n) ` ` ` `{ ` ` ` `return` `((` `int` `)(Math.Log(n & -n) / Math.Log(2)) ` ` ` `+ 1) - 1; ` ` ` `} ` ` ` ` ` `// function to find the next greater integer ` ` ` `static` `int` `nextGreaterWithOneMoreSetBit(` `int` `n) ` ` ` `{ ` ` ` ` ` `// position of rightmost unset bit of n ` ` ` `// by passing ~n as argument ` ` ` `int` `pos = getFirstSetBitPos(~n); ` ` ` ` ` `// if n consists of unset bits, then ` ` ` `// set the rightmost unset bit ` ` ` `if` `(pos > -1) ` ` ` `return` `(1 << pos) | n; ` ` ` ` ` `// n does not consists of unset bits ` ` ` `return` `((n << 1) + 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `Console.Write(` `"Next greater integer = "` ` ` `+ nextGreaterWithOneMoreSetBit(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find the ` `// next greater integer with ` `// one more number of set bits ` ` ` `// function to find the position ` `// of rightmost set bit. Returns ` `// -1 if there are no set bits ` ` ` `function` `getFirstSetBitPos(` `$n` `) ` `{ ` ` ` `return` `(log(` `$n` `& -` `$n` `+ 1)) - 1; ` `} ` ` ` `// function to find the ` `// next greater integer ` ` ` `function` `nextGreaterWithOneMoreSetBit(` `$n` `) ` `{ ` ` ` `// position of rightmost unset bit of n ` ` ` `// by passing ~n as argument ` ` ` ` ` `$pos` `= getFirstSetBitPos(~` `$n` `); ` ` ` ` ` `// if n consists of unset bits, then ` ` ` `// set the rightmost unset bit ` ` ` `if` `(` `$pos` `> -1) ` ` ` `return` `(1 << ` `$pos` `) | ` `$n` `; ` ` ` ` ` `//n does not consists of unset bits ` ` ` `return` `((` `$n` `<< 1) + 1); ` `} ` ` ` `// Driver Code ` `$n` `= 10; ` `echo` `"Next greater integer = "` `, ` ` ` `nextGreaterWithOneMoreSetBit(` `$n` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

Output :

Output :

Next greater integer = 11

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the largest number smaller than integer N with maximum number of set bits
- Previous smaller integer having one less number of set bits
- Closest (or Next) smaller and greater numbers with same number of set bits
- How to swap two bits in a given integer?
- Count set bits in an integer
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Count set bits in an integer using Lookup Table
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Check if a number has same number of set and unset bits
- Set all odd bits of a number
- Same Number Of Set Bits As N
- Set all even bits of a number
- Change all even bits in a number to 0
- Rotate bits of a number
- Toggle first and last bits of a number
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle all the bits of a number except k-th bit.