Program to find the Nth natural number with exactly two bits set | C ++ Code to find the Nth number with exactly two bits set ; Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNthNum ( long long int N ) { long long int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; cout << ( 1 << bit_L ) + ( 1 << bit_R ) << endl ; } int main ( ) { long long int N = 13 ; findNthNum ( N ) ; return 0 ; }