bitcount.c

This is a discussion on bitcount.c within the C Programming forums, part of the General Programming Boards category; I am trying to get this to read a files integers and print the number of 1 bits in each ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    8

    bitcount.c

    I am trying to get this to read a files integers and print the number of 1 bits in each integer. The file I am redirecting as input to test with has 100 in it. But the output I'm getting is:

    Number of bits set to 1 in 49 = 3


    Number of bits set to 1 in 48 = 2


    Number of bits set to 1 in 48 = 2


    Number of bits set to 1 in 10 = 2

    I'd like to be getting Number of bits set to 1 in 100 = 3

    Not sure if the while loop is correctly coded or what, can you help out?

    Code:
    #include <stdio.h>
    #define EOF (-1)
    unsigned char bitcount(unsigned char);
    
    main()
    {
      int i;
      unsigned char count, i8;
    
     while ((i = getchar() ) != EOF) 
     {
       if ((i < 0) || (i > 255)) { 
         printf("Number out of range = %d\n", i);
         exit(1);
       }
       else
       {
       i8 = (unsigned char) i;
       count = bitcount(i8);   
    
       printf("\n\nNumber of bits set to 1 in %d = %d\n",i,count);
       }//end else
     } //end while
    } //end main
    
    
    /* bitcount: count 1 bits in x */
    unsigned char bitcount(unsigned char x)
    {
      unsigned char count;
    
      for (count = 0; x != 0; x >>=1)
       if (x & 01)
        count++;
      return count;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're reading the numbers as ASCII characters, not the actual numeric values. Entering 100 will actually be interpreted as '1', '0', '0', '\n'. I suggest you use fgets instead of getchar and convert the input string to a number using strtol.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    That's because you're using getchar to read input. It reads the '1' char and converts that to its ASCII value of 49, then it reads the two '0' characters and converts those both to 48, the value 10 probably happens because there is a newline character in the redirected file. Bottom-line is you should use some other method of getting input. Perhaps scanf with %d format specifier to read an actual integer instead of characters.

    [edit]Oops, too late![/edit]
    "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
    Feb 2010
    Posts
    8
    Ah, understand what it was doing with the ASCII now.

    This is what I previously had...
    Code:
    #include <stdio.h>
    
    unsigned char bitcount(unsigned char); 
    
    main()
    {  
       unsigned char i8,count;
       int i;
    
      printf("Enter number (0 - 255 decimal)\n");  
      scanf("%d",&i);
       
    
         if (( i < 0 ) || (i > 255))
         {
          printf("Error:Number out of range = %d\n", i);
          exit(1);
         }
      
       i8 = (unsigned char) i;
       
       count = bitcount(i8);
       
       printf("\n\nNumber of bits set to 1 in %d = %d\n",i,count);
       
    }
    
    unsigned char bitcount(unsigned char x)
    
    {  
    unsigned char count;
    
       for (count = 0; x!=0; x>>=1)
         if ( x & 01 )
           count++;
           
      return count;
    }
    but the instructor wants us to "not prompt the user for the amount of input, but just process the data until the end of input. If you create a file called mydata.txt with the list of numbers to test, you should be able to invoke your code as:
    ./bitcount.exe < mydata.txt
    Last edited by kezkez; 06-22-2010 at 11:41 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could use strtol() to convert the string number to an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by kezkez View Post
    but the instructor wants us to "not prompt the user for the amount of input, but just process the data until the end of input. If you create a file called mydata.txt with the list of numbers to test, you should be able to invoke your code as:
    ./bitcount.exe < mydata.txt
    Then don't prompt the user for input and enclose the scanf() and other statements, excluding the last printf(), inside a loop.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    This is what I've come up with so far. I got it to work for the first string for example where the file mydata.txt contains: 123 . But if the mydata file contains a second string I cannot get it to work for example: 123 456 it will read 123 but not 456. What am I missing?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int bitcount(int);
    
    main()
    {
     char i;
    
     int count,i8;
    
      while( i != EOF)
      {
        scanf("%d",&i);
           
        i8 = (int) i; 
    
        count = bitcount(i8);
     
       printf("\n\nNumber of bits set to 1 in %d = %d\n",i,count);
    return 0;
      }//end while
    }//end main
    
    /* bitcount */
    int bitcount(int x)
    {
      int count;
      for (count = 0; x !=0; x >>= 1)
       if (x & 01)
       count++;
      return count;
    }
    output:
    Number of bits set to 1 in 123 = 6

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    scanf("%d",&i);
    [/color] i;
    Why are you using %d to read into a char? Why isn't i an int?


    Quzah.
    Last edited by quzah; 06-24-2010 at 02:09 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    ok, i did that, but any suggestions on how to get the loop to work for more that one string?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int bitcount(int);
    main() {
     int count, i; 
      while( i != EOF)
      {
        scanf("%d",&i); 
        count = bitcount(i);
       printf("\n\nNumber of bits set to 1 in %d = %d\n",i,count);
    return 0;
      }//end while
    }//end main
    
    /* bitcount */
    int bitcount(int x){
      int count;
      for (count = 0; x !=0; x >>= 1)
       if (x & 01)
       count++;
      return count;
    }
    Last edited by kezkez; 06-25-2010 at 09:12 AM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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