Count same bits in all remaining blocks .
while ( $ n > 0 ) { $ first_bit = $ n % 2 ; $ curr_count = 1 ; $ n = ( int ) ( $ n / 2 ) ; while ( $ n % 2 == $ first_bit ) { $ n = ( int ) ( $ n / 2 ) ; $ curr_count ++ ; } if ( $ curr_count != $ first_count ) return false ; } return true ; }