All 1's on one side

Program to see if all the ones in a number appear on the right side of the number and all zeros appear on the left, how can I do this most efficiently? (i.e. 00000111 is true but 100010 is false)

Comments

  1. //add 1 and check if the result is a power of 2.

    int check(int n){
    if(!(n&(n+1)))return 1;
    return 0;
    }

    ReplyDelete
  2. @jainendra exacty perfect solution, you can even avoid if condition,you can put the same condition in return statement.

    ReplyDelete

Post a Comment

Popular posts from this blog