Thread: Binary Representation

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    Binary Representation

    Code:
    void printOnes (int x) 
    { 
      int i; 
      int numOnes = 0; 
      for (0 = i; i < 32; i++) { 
        if ((x & 0x1) = 1) 
          numOnes++; 
        x = x >> 1; 
      } 
      printf("The number of ones is %c\n", numOnes); 
    }
    This is supposed to count the number of 1s in the binary representation of an integer. I know there are some logical errors.

    I know the logical operator x & 0x1 simply seeing if it's odd but I don't see how this counts the number of 1s. (isn't is just saying x % 2 == 1. so x could be 3,5..etc) How would I fix this?

    or is x=x>>1 wrong?
    Last edited by clag; 01-19-2010 at 01:41 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    = 1 should be == 1
    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"

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
      for (0 = i; i < 32; i++) {
    Does that even compile?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
      for (0 = i; i < 32; i++) {
    Does that even compile?
    Wait, what's wrong with changing The World so that 0 gets another value? ;-)

    Really, the two problems have been pointed out. I just wanted to note that there is another algorithm that can be faster:
    Code:
    count = 0
    while number != 0
      number = number & (number - 1)
      count++
    Actually, it's a well known algorithm. But I don't know if it's actually faster. It requires less steps (or as many steps) to find the answer, but decrementing might require more time than decrementing...
    Anyways, it's just informative, it's a fun algorithm. See if you can figure out how it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary representation in C
    By @nthony in forum C Programming
    Replies: 25
    Last Post: 11-10-2007, 12:43 AM
  2. Replies: 3
    Last Post: 11-25-2006, 04:14 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Binary representation similar to 0xFF ?
    By DougDbug in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2003, 12:08 PM