checking if a number if a power of 2

This is a discussion on checking if a number if a power of 2 within the C Programming forums, part of the General Programming Boards category; Here's my code: Code: ((num & (num - 1)) == 0) is this true? I tried it and it doesn't ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    checking if a number if a power of 2

    Here's my code:

    Code:
     ((num & (num - 1)) == 0)
    is this true? I tried it and it doesn't work as it's supposed to be. So is there another easier way?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Check if the square root is a whole number.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It should work. What numbers are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by whiteflags View Post
    Check if the square root is a whole number.
    And thus, 25 is a power of 2?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am trying to use 3

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    I am trying to use 3
    And what result to you expect, and how is that different from what you get? 3 is not a power of 2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh nvm.. I found my self a stupid little mistake

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by brewbuck View Post
    And thus, 25 is a power of 2?
    Isn't it? 5 squared...

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Power of two means two raised to a power, not a number raised to the second power. It's 2^x not x^2.

    edit: The reason that first equation works is that any power of a number will be 1000 in that number system. 10, 100, 1000 are all powers of ten in the base-ten system. In base-two, they are all powers of two. Subtract one, and there will be a one in place of all the zeroes. 100 - 1 = 011. 100 & 011 = 0.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by whiteflags View Post
    Isn't it? 5 squared...
    ... Making it a power of 5.

    Can you slap your forehead for me? I can't reach you
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by iMalc View Post
    ... Making it a power of 5.
    Which reminds me suddenly:

    "Power of 3
    Make us free,
    Power of 3
    Make us free..."

    (c) Charmed

    iMalc, do you need some power of 3 to reach him remotely?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regarding Nth Power of a Number
    By Shameer in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2010, 10:44 PM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. checking string if number or not
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-17-2006, 09:08 AM
  4. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 01:46 AM
  5. random number checking...
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21