AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: Ashley — October 6, 2006
    -5 votes
      + -

    if (x ^ (x-1) == 0 then its a power of 2

  2. Submitted By: smnaha — October 6, 2006
    -2 votes
      + -

    This works for zero also:

    if (x && ~(x & (x-1)) == 0)

  3. Submitted By: pt1000 — October 6, 2006
    -9 votes
      + -

    I don’t think that Ashley’s plan will work.

    (X ^ (X-1)) will never equal 0 for a positive integer

    1^0 = 1
    2^1 = 2
    3^2 = 9
    etc..

  4. Submitted By: vadim — October 6, 2006
    -2 votes
      + -

    if ((x && (xx-1)) == 0) {} //x is power of 2

  5. Submitted By: ashish_ranjan — October 6, 2006
    -4 votes
      + -

    this will work i suppose (assuming x!= 0)
    if( ~(x^(x-1))== 0) then it is power of 2
    (^ :exclusive or)

  6. Submitted By: lmauro — October 6, 2006
    -4 votes
      + -

    have you tried this solutions in a loop ?
    they don’t seem to work, but inspired on them, this does:

    (x ^(x-1))-x > 0

    will be 1 (true) for all powers of 2 including 0.

  7. Submitted By: jinaljhaveri — October 6, 2006
    +46 votes
      + -

    ALL THE ABOVE ARE WRONG
    THE CORRECT ONE IS
    if(!(x&(x-1)))

  8. Submitted By: c sharp — October 6, 2006
    -2 votes
      + -

    maxPower2 = (~0 >>> 1) + 1
    if (0 == (maxPower2 % x)) then x is power 2

  9. Submitted By: c sharp — October 6, 2006
    -1 votes
      + -

    My previous post probably is not clear.

    (2^n % x) == 0 only if x is power 2

    Maximum 2^n = ((unsigned)~0 >> 1) + 1 ==
    1000000..0

    if (0 == (((unsigned)~0 >> 1) + 1) % x)) then x is power 2

  10. Submitted By: octav_timofte — October 6, 2006
    -1 votes
      + -

    . The binary representation of any integer number except 0 will contain at least a bit 1. Taking into account this property we can represent a number like this:
    X = b1b2….bk10…0t
    Note: the number t of trailing zeros can be zero and also the number k of leading bits can be zero.

    Let first assume that X is an unsigned integer. Then if the number is not a power of two at least one bit from (bi) sequence must be 1. If we represent in binary form the number X-1 we get:
    X-1 = b1b2….bk01…1t
    Looking at these form it is obvious that X & (X-1) = b1b2….bk001…0t . Only for a power a power of two bi=0, for each i so we have X & (X-1) = 0 only for a power of two.
    The macro to test the number can be: #define pow2(X) (!(X&(X-1)))

    For X = b1b2….bk101…0t then –X=c1c2….ck10…0t where ci=1-bi (the negative number is represent as the complement of 2). For a power of two ci=1, for each i and it is difficult to test it so for a negative number it easier to test its absolute value. The macro will be extented like this: #define pow2(X) (X>0)?(!((X)&(X-1))): (!((-X)&(-X-1)))

    Let take now a closer view for the case X=0. This case was excluded at the beginning when we assumed we had at least on bit 1. 0 can’t be a power of two so the macro will be extented again to take care of 0.

    #define pow2(X) (X!=0 && ((X>0)?(!((X)&(X-1))): (!((-X)&(-X-1)))))

  11. Submitted By: helloworld — October 6, 2006
    -1 votes
      + -

    Try this out :

    if( (N !=0) && ((2*N-1) ^ (N <<1)) == ((2*N-1)

  12. Submitted By: friend — October 6, 2006
    -3 votes
      + -

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    unsigned int x;
    int base,j;
    clrscr();
    //Program to clear MSB of the number
    printf(”nEnter value of x = “);
    scanf(”%u”,&x);
    x=x&(0×7fff);
    printf(”nNew value of x = %u”,x);

    //Program for testing whether a no. is power of 2

    printf(”nEnter value of x = “);
    scanf(”%u”,&x);
    if(x&x-1)
    printf(”nNumber is not power of 2 “);
    else
    printf(”nNumber is power of 2 “);

    //Program to find discrete log of the number;
    printf(”nEnter the number = “);
    scanf(”%u”,&x);
    printf(”nEnter the base = “);
    scanf(”%u”,&base);
    j=1;
    while(x>=base)
    {
    x=x/base;
    j++;
    }
    printf(”nLog x = %u”,j-1);

    // Reversing bits of the number
    printf(”nEnter the number = “);
    scanf(”%u”,&x);

    getch();
    }

  13. Submitted By: cherku — October 6, 2006
    +0 votes
      + -

    #include <stdio.h>
    main(){
    int num;

    printf(” Enter number :”);
    scanf(”%d”,&num);
    if(!(num & (num-1)))
    printf(”Power”);
    else
    printf(”Not power”);
    }

  14. Submitted By: don — October 6, 2006
    +0 votes
      + -

    if((log(number)/log(2))%2==0)
    {
    power of 2;
    }
    else
    {
    not;
    }

  15. Submitted By: jon — October 6, 2006
    +2 votes
      + -

    smnaha’s solution is not correct

    my solution is alomst same as Ashley’s

    if((x&(x-1))==0)

    if x is power of 2 it will have only 1 bit on at the location n , where x = 2 pow n

    n-1 will have bits 0 to n-1 on and rest will be off

    so bitwise and(&) of x , x-1 should be 0 only if x is a power of 2.

    Ashley’s using XOR instead of AND which is also correct.

  16. Submitted By: rdakka — October 6, 2006
    -1 votes
      + -

    bool pof2(unsigned x)
    {
    return (x && (x & (x-1)) == 0) ;
    }

  17. Submitted By: tolymenn — October 6, 2006
    +2 votes
      + -

    I think all the answers here are either wrong completly, partially, or too complex. The best answer so far, by jinaljhaveri, was if(!(x&(x-1))), which I beleive works for all powers of 2, but also for 0, which is not a power of 2.

    Basiclly, an integer which is the power of 2 will have one and only one bit set in its binary representation. If you subtract 1 from that, you will get all bits set (equal to 1) before the original 1 bit, and the rest are 0. Taking advantage of this, the answer is

    if ((x << 1) == 1 + (x

  18. Submitted By: rituvarm — October 6, 2006
    +0 votes
      + -

    it also cud be without using any bitwise operators

    if(x!=0 && ((x/2)%2)==0)
    printf(”x is a power of 2″);

  19. Submitted By: rituvarm — October 6, 2006
    -2 votes
      + -

    sorry the above one misses something

    if(x!=0 && ((x/2)%2)==0 && (x%2)==0)
    printf(”x is a power of 2″);

  20. Submitted By: nish_parikh — October 6, 2006
    -1 votes
      + -

    In response to answer posted by rituvarm,

    Even 12 is a power of 2 according to your equation?

  21. Submitted By: nish_parikh — October 6, 2006
    +1 votes
      + -

    To solve w/o bitwise operators

    while(x%2==0 && (x/=2)!=1 && x!=0);
    return(!(x-1));

    returns true if x is a power of 2
    else returns false

    considers 0 not to be a power of 2.

  22. Submitted By: rituvarm — October 6, 2006
    not yet rated
      + -

    yes u are rite..
    mistake!

  23. Submitted By: rituvarm — October 6, 2006
    not yet rated
      + -

    but u cant usea loop nish…u used awhile loop
    cud u tell me without usinga loop please..
    thnks

  24. Submitted By: KRogoway — October 6, 2006
    -3 votes
      + -

    vadim, jinaljhaveri, octav_timofte

    I do not think you have the correct answers.

    This is what you are all basing your solutions on:

    Some form of: X&(X-1)

    This falls apart for any number that is the sum of two powers of two (e.g. (4+2)=6 ).

    Example:
    X = 6

    X & (X-1) ==> 6&(6-1) ==> 6&5 != 0

    According to your solutions, 6 would be a power of two. Keep trying guys!

  25. Submitted By: KRogoway — October 6, 2006
    not yet rated
      + -

    KRogoway (myself) is a dorkus. Your solutions are correct, my check was incorrect.

    Sorry guys.

  26. Submitted By: Dumb — December 2, 2006
    -1 votes
      + -

    First Solution
    ————-
    int PowerofTwo(int x)
    {
    return ((x^(x-1)) == 2*x-1) ;
    }

    Second Solution
    —————
    int PowerofTwo(int x)
    {
    return (!(x&(x-1))) ;
    }

    Regards ;)

  27. Submitted By: raushan — December 14, 2006
    -1 votes
      + -

    if the no is power of 2 then only MSB will be 1 when converted to binary….
    2=10
    4=100…
    so do a left shift on this no and if it becomes 0 dfntly thats power of 2….
    if ( n

  28. Submitted By: raushan — December 14, 2006
    -1 votes
      + -

    for the above soln. the variable should be stored in such a way that it is provided with just the amount of memory it is required .. not more..
    only in that case one left shift will do the job….
    else if ( ~(x^(x-1))) == 0) will be fine

  29. Submitted By: Mavericktua — April 24, 2007
    +1 votes
      + -

    Take the log of the number and Check whether the result in an integer or not.

  30. Submitted By: Nick — May 29, 2007
    not yet rated
      + -

    Cheap solution.
    And the number to be tested with the a number that is one less than that number. By properties of bitvectors the result should be 0.

  31. Submitted By: Nick — May 29, 2007
    not yet rated
      + -

    The answer works for 0 also because in 2’s complement -1 is represented as 1111……….

  32. Submitted By: wireless s/w — June 6, 2007
    not yet rated
      + -

    Whats wrong with this logic if x is positive:

    if ~(x-1) = 0, then x is a power of two

  33. Submitted By: Xu Mi Hu Ha Lee Ye Wan — June 7, 2007
    +3 votes
      + -

    int result = ((x&(x-1))==0&&x)?1:0;

    Just one line. result = 1 means True,result = 1 means False…

  34. Submitted By: subhashish — June 20, 2007
    not yet rated
      + -

    if the number is x then,

    if ( (x & (x-1)) == 0)
    {
    its a power of 2
    }

  35. Submitted By: Krups — October 23, 2007
    -1 votes
      + -

    left shift by one, (BITWISE) and compare to 0, if true, power of 2!

  36. Submitted By: berhe — April 21, 2008
    +1 votes
      + -

    Easy soln
    Every pow of two number is a nth shift of 1,
    1= 1 if ((n | (n - 1)) + 1 == 2 * n) -> n is pow of 2

  37. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.