Fibbinary Numbers ( No consecutive 1 s in binary ) | CPP program to check if a number is fibinnary number or not ; function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code to check above function
#include <iostream> NEW_LINE using namespace std ; bool checkFibinnary ( int n ) { int prev_last = 0 ; while ( n ) { if ( ( n & 1 ) && prev_last ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } int main ( ) { int n = 10 ; if ( checkFibinnary ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }