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);
}