Check if a number has bits in alternate pattern | Set 1 | C ++ program to find if a number has alternate bit pattern ; Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPattern ( int n ) { int prev = n % 2 ; n = n / 2 ; while ( n > 0 ) { int curr = n % 2 ; if ( curr == prev ) return false ; prev = curr ; n = n / 2 ; } return true ; } int main ( ) { int n = 10 ; if ( findPattern ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }