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

This is a discussion on need help debugging a program counting bits set from an integer number within the C Programming forums, part of the General Programming Boards category; The program is supposed to count the number of bits set by an integer. for example: 5 is 00000101 and ...

  1. #1
    bpf
    bpf is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    7

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

    The program is supposed to count the number of bits set by an integer. for example: 5 is 00000101 and has two bits set. compiling the program with debug statements makes me think that the program isn't even entering the integer into the loop

    Code:
    /********************************************************
     *							*
     * FILE: ch10_ex4.c					*
     * CREATED: 8/15/07					*
     * BY: bpf						*
     *							*
     *  this program counts the number of bits set in an 	*
     *   integer						*
     *							*
     ********************************************************/
    
    #include<stdio.h>
    
    main()
    {
      char line[100];
      int itgr; /* the integer value of the input number */
    
      int r; /* remainder */
      int set = 0; /* number of variables set */
      int n; /* index into the line of bits */
      int subt = 0; /* the value to subtract from the remainder starts at zero each loop */
    
      /* input number */
      (void)printf("enter an integer: ");
      (void)fgets(line, sizeof(line), stdin);
      (void)sscanf(line, "%d", &itgr);
    
      r = itgr; /* remainder starts off the same as the integer */
    
      while(0)
        {      
          /* if the remainder is equal to one, the one's column is set and the loop can stop */
          if (r == 1)
    	{
    	  set++;
    
    	  break;
    	}
    
          /* if the remainder is equal to zero the number of bits set are counted and the loop stops */
          else{ if (r == 0)
    	  {
    	    break;
    	  }
    
    	/* the highest multiple of two is counted up to and becomes the number subtracted from the remainder */      
    	else{ for (n = 1; n <= r; n * 2)
    	    {
    	      subt = n;
    	    }
    
    	  /* the remainder is lowered and one column is set */	  
    	  r = r - subt;
    	  set++;
    	}
          }
        }
    
      /* how many bits are set */
      (void)printf("%d bits are set\n", set);
    
      return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    bpf
    bpf is offline
    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

  4. #4
    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

  5. #5
    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.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
     while(itgr)
      {
          if(itgr&#37;2 == 1)
             set++;
          
          itgr /= 2;   
      }
    You could replace your while loop with this one. This minimizes your code. And why do want to type cast the printf return value to void. There is no need for that.

    ssharish2005

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    lol while(0) is ironic. (you'd think he wanted to use while since he put it in there, but apparently not.)

    it's like thinking "i'm going to open the door" while standing in front of the door doing nothing.

    ...lol


    while(1) is ... probably more effective.
    Last edited by simpleid; 08-17-2007 at 04:24 PM.

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    you can just use // to comment out, saves time and fingers moving around to do /* and */ everytime u want to comment

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, 12:16 PM
  3. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09: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-26-2001, 12:45 AM

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