Given a number N. The task is to find the smallest number which is greater than N and has only one bit different in the binary representation of N.

**Note**: Here N can be very large 10^9 < N < 10^15.

**Examples:**

Input: N = 11Output: The next number is 15 The binary representation of 11 is 1011 So the smallest number greater than 11 with one bit different is 1111 which is 15.Input: N = 17Output: The next number is 19

**Simple Approach**: We will run a loop from N+1 until we find a number which has exactly one bit different than N. This process might take a long time to process in case of large numbers

**Efficient Approach**: In the efficient approach we have to represent the number in its binary form. Now a number greater than N with only 1 bit different is only possible when we keep all the set bits of the number N intact and switch the lowest possible bit which is zero to 1.

Let us take the example, 1001 which is binary form of 9,

If we switch the set bits of 1001 to either 1000 or 0001 then we find that the numbers are 8 and 1 which are less than N. While if we turn the bits which are zero to 1 we find the numbers are 11 (1011) or 13 (1101) so if switch the lowest possible bit which is zero to 1 then we get the smallest possible number greater than N with only 1 bit different, in this case it is 11 (1011).

**Note**: It is guaranteed that the input number N does not have all of its bits as set. There exists at least one unset bit in the binary representation of N.

Below is the implementation of the above approach:

## C++

`// CPP program to find next greater number than N ` `// with exactly one bit different in binary ` `// representation of N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find next greater number than N ` `// with exactly one bit different in binary ` `// representation of N ` `long` `long` `nextGreater(` `long` `long` `N) ` `{ ` ` ` `long` `long` `power_of_2 = 1, shift_count = 0; ` ` ` ` ` `// It is guaranteed that there ` ` ` `// is a bit zero in the number ` ` ` `while` `(` `true` `) { ` ` ` `// If the shifted bit is zero then break ` ` ` `if` `(((N >> shift_count) & 1) % 2 == 0) ` ` ` `break` `; ` ` ` ` ` `// increase the bit shift ` ` ` `shift_count++; ` ` ` ` ` `// increase the power of 2 ` ` ` `power_of_2 = power_of_2 * 2; ` ` ` `} ` ` ` ` ` `// set the lowest bit of the number ` ` ` `return` `(N + power_of_2); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `N = 11; ` ` ` ` ` `// display the next number ` ` ` `cout << ` `"The next number is = "` `<< nextGreater(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find next greater number than N ` `// with exactly one bit different in binary ` `// representation of N ` ` ` `class` `GFG{ ` `// Function to find next greater number than N ` `// with exactly one bit different in binary ` `// representation of N ` `static` `int` `nextGreater(` `int` `N) ` `{ ` ` ` `int` `power_of_2 = ` `1` `, shift_count = ` `0` `; ` ` ` ` ` `// It is guaranteed that there ` ` ` `// is a bit zero in the number ` ` ` `while` `(` `true` `) { ` ` ` `// If the shifted bit is zero then break ` ` ` `if` `(((N >> shift_count) & ` `1` `) % ` `2` `== ` `0` `) ` ` ` `break` `; ` ` ` ` ` `// increase the bit shift ` ` ` `shift_count++; ` ` ` ` ` `// increase the power of 2 ` ` ` `power_of_2 = power_of_2 * ` `2` `; ` ` ` `} ` ` ` ` ` `// set the lowest bit of the number ` ` ` `return` `(N + power_of_2); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[]a) ` `{ ` ` ` `int` `N = ` `11` `; ` ` ` ` ` `// display the next number ` ` ` `System.out.println(` `"The next number is = "` `+ nextGreater(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find next greater ` `# number than N with exactly one ` `# bit different in binary ` `# representation of N ` ` ` `# Function to find next greater ` `# number than N with exactly ` `# one bit different in binary ` `# representation of N ` `def` `nextGreater(N): ` ` ` ` ` `power_of_2 ` `=` `1` `; ` ` ` `shift_count ` `=` `0` `; ` ` ` ` ` `# It is guaranteed that there ` ` ` `# is a bit zero in the number ` ` ` `while` `(` `True` `): ` ` ` ` ` `# If the shifted bit is ` ` ` `# zero then break ` ` ` `if` `(((N >> shift_count) & ` `1` `) ` `%` `2` `=` `=` `0` `): ` ` ` `break` `; ` ` ` ` ` `# increase the bit shift ` ` ` `shift_count ` `+` `=` `1` `; ` ` ` ` ` `# increase the power of 2 ` ` ` `power_of_2 ` `=` `power_of_2 ` `*` `2` `; ` ` ` ` ` `# set the lowest bit ` ` ` `# of the number ` ` ` `return` `(N ` `+` `power_of_2); ` ` ` `# Driver code ` `N ` `=` `11` `; ` ` ` `# display the next number ` `print` `(` `"The next number is ="` `, ` ` ` `nextGreater(N)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find next ` `// greater number than N with ` `// exactly one bit different in ` `// binary representation of N ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to find next greater ` `// number than N with exactly ` `// one bit different in binary ` `// representation of N ` `static` `int` `nextGreater(` `int` `N) ` `{ ` ` ` `int` `power_of_2 = 1, ` ` ` `shift_count = 0; ` ` ` ` ` `// It is guaranteed that there ` ` ` `// is a bit zero in the number ` ` ` `while` `(` `true` `) ` ` ` `{ ` ` ` `// If the shifted bit is ` ` ` `// zero then break ` ` ` `if` `(((N >> shift_count) & 1) % 2 == 0) ` ` ` `break` `; ` ` ` ` ` `// increase the bit shift ` ` ` `shift_count++; ` ` ` ` ` `// increase the power of 2 ` ` ` `power_of_2 = power_of_2 * 2; ` ` ` `} ` ` ` ` ` `// set the lowest bit ` ` ` `// of the number ` ` ` `return` `(N + power_of_2); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 11; ` ` ` ` ` `// display the next number ` ` ` `Console.WriteLine(` `"The next number is = "` `+ ` ` ` `nextGreater(N)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find next greater ` `// number than N with exactly one ` `// bit different in binary ` `// representation of N ` ` ` `// Function to find next greater ` `// number than N with exactly ` `// one bit different in binary ` `// representation of N ` `function` `nextGreater(` `$N` `) ` `{ ` ` ` `$power_of_2` `= 1; ` ` ` `$shift_count` `= 0; ` ` ` ` ` `// It is guaranteed that there ` ` ` `// is a bit zero in the number ` ` ` `while` `(true) ` ` ` `{ ` ` ` `// If the shifted bit is ` ` ` `// zero then break ` ` ` `if` `(((` `$N` `>> ` `$shift_count` `) & 1) % 2 == 0) ` ` ` `break` `; ` ` ` ` ` `// increase the bit shift ` ` ` `$shift_count` `++; ` ` ` ` ` `// increase the power of 2 ` ` ` `$power_of_2` `= ` `$power_of_2` `* 2; ` ` ` `} ` ` ` ` ` `// set the lowest bit of the number ` ` ` `return` `(` `$N` `+ ` `$power_of_2` `); ` `} ` ` ` `// Driver code ` `$N` `= 11; ` ` ` `// display the next number ` `echo` `"The next number is = "` `, ` ` ` `nextGreater(` `$N` `); ` ` ` `// This code is contributed ` `// by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The next number is = 15

**Time Complexity:** O(log(N))

## Recommended Posts:

- Find next greater element with no consecutive 1 in it's binary representation
- Smallest subarray of size greater than K with sum greater than a given value
- Smallest multiple of N with exactly N digits in its Binary number representation
- Next greater integer having one more number of set bits
- Find the Next perfect square greater than a given number
- Next greater Number than N with the same quantity of digits A and B
- Find a number K such that exactly K array elements are greater than or equal to K
- Elements greater than the previous and next element in an Array
- Find the next Factorial greater than N
- Count of Array elements greater than all elements on its left and next K elements on its right
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Find value of k-th bit in binary representation
- 1 to n bit numbers with no consecutive 1s in binary representation
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Smallest integer greater than n such that it consists of digit m exactly k times
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Count of 1-bit and 2-bit characters in the given binary string
- Find longest sequence of 1's in binary representation with one flip

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.