Thread: need help debugging a program counting bits set from an integer number

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    How many times do you think while(0) will execute ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    until the remainder goes to 1 or zero

    so the for loop will find the highest multiple of two and subtract it from the remainder until it reaches the 'one's' spot

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What exactly is the condition inside the parenthesis of "while(0)", and what does that evaluate to as a "true/false" statement?

    --
    Mats

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    What basically people are trying you to answer is that 0 means "false" in C and everything else means "true". So, by writing while(0), you are basically saying "while false" which, of course, evalute to false...

    And by the way, this is an infinite loop if r > 1
    Code:
    for (n = 1; n <= r; n * 2)
    {
        subt = n;
    }
    instead, you should write something like
    Code:
    for (n = 1; n <= r; n = n * 2)
    {
        subt = n;
    }
    Also, you could rewrite your code so it have a nicer and easier to understand structure/logic. But it's not that bad either...

    Also, you might want to check the return value of sscanf and check if the number entered by the user is non negative before entering your loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  3. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  4. program error
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-15-2001, 10:20 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM