Thread: C Read an integer and count 1s only at positions 2, 4, 6 , 8 of the binary

  1. #16
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    OK I solved it. As I said I had to solve that only with ONE variable, and count only the bits at positions 2,4,6,8 of a binary code, not ALL THE BITS IN A BINARY CODE. So I CANNOT use a function, or for loops or while loops, because I would need to use more variables. Here is my code:


    Code:
    #include <stdio.h>
    int main()
    {
        int x ;
    
    
    	printf("Enter number: ");
    	scanf("%d",&x);
    	
    printf("%d\n", x & 1);  //pos 1
    printf("%d\n", (x >> 1) & 1); //pos 2
    printf("%d\n", (x >> 2) & 1); //pos 3
    printf("%d\n", (x >> 3) & 1); //pos 4
    printf("%d\n", (x >> 4) & 1); //pos 5	
    printf("%d\n", (x >> 5) & 1); //pos 6	    
    printf("%d\n", (x >> 6) & 1); //pos 7
    printf("%d\n", (x >> 7) & 1); //pos 8	
    
    
    printf("\n\nTotal: %d\n", ((x >> 1) & 1)+((x >> 3) & 1)+((x >> 5) & 1)+((x >> 7) & 1));
    	
    	return 0;
    }

    THANK YOU ALL FOR TRYING TO HELP ME !

  2. #17
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hi askis!

    May be this example below can help you:
    Code:
    /** tallybit.c read in a integer value, as a 4-Byte(32Bit) integer
      * and counts the digits where are set and not set.
      * If you read in a negativ integer it shows how does it looks
      * in binary(Two's complement)
      * more information at
      * https://en.wikipedia.org/wiki/Signed_number_representations
      *
      * written by Josef Franz Wismeth, in upper palatinate, bavaria
      *
      * last changes at 2018 December, 26th */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /** counts the number of bits of argument 1, argument_2 = number of bits are set, argument_2 number of bits are not set*/
    void tallybin(int intnumber, unsigned int (*bits_set), unsigned int (*bits_not_set))
    {
     unsigned int i, grot = 0, result, bits_are_on = 0, bits_are_off = 0, bitsize = 0;
    
      bitsize = ((sizeof(int)) * 8);
    
      grot = 1 << (bitsize - 1);
    
     /// can set the line to a comment
     printf("size of argument 1 in bit: %d\n", bitsize);
    
    
    for (i = 0; i <= (bitsize - 1); i++)
     {
      result = (unsigned int)intnumber & grot;
    
      /// shows the bits, can set this two lines to a comment
      if (result) printf("1");
        else printf("0");
    
      grot >>= 1;
      if (result) bits_are_on++;
       else bits_are_off++;
      }
    
     (*bits_set) = bits_are_on;
     (*bits_not_set) = bits_are_off;
    }
    
    
    int main()
    {
     int number = 0;
     unsigned int bits_on = 0, bits_off = 0;
    
     printf("Counting the bits!\n");
     printf("Please enter a integer value: ");
     scanf("%d", & number);
     printf("\nThe value was: %d\n", number);
    
     tallybin(number, &bits_on, &bits_off);
    
    
    
     printf("\nNumber of bits are set: %u\n", bits_on);
     printf("Number of bits not set: %u\n", bits_off);
    
    
     return 0;
    }
    Hey Mister Tallyman telly me banana...........

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by akis View Post
    OK I solved it.
    There is an alternative method, but it requires a bit of insight to sum up bit fields in parallel.

    Code:
        #include <stdio.h>
        int main()
        {
            int x ;
            printf("Enter number: ");
            scanf("%d",&x);
            x = (x & 0xaa) >> 1;
            x = (x >> 4) + (x & 0xf);
            x = (x >> 2) + (x & 0x3);
            printf("sum: %d\n", x);
            return 0;
        }
    Last edited by rcgldr; 12-28-2018 at 11:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Append an integer to a char * as a count
    By googol in forum C Programming
    Replies: 5
    Last Post: 04-15-2014, 10:12 PM
  2. read file start from different positions
    By Dedalus in forum C Programming
    Replies: 7
    Last Post: 08-21-2011, 01:35 PM
  3. read roll no., marks, count pass and fail
    By jackson6612 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2011, 11:51 PM
  4. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 07:18 PM
  5. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM

Tags for this Thread