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

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

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

    I need help at this problem:
    I want a C program that reads an integer and counts the sum of digits at the positions 2, 4, 6, 8 of it's binary code. I have to use only one variable. For example if the user enters the integer 170 (10101010 in binary) the program should show 4, since at positions 2, 4, 6, 8 of the binary there are ones. (This program should be solved with the use of bitwise operators and bit shiftings)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your idea and what have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    13

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

    Quote Originally Posted by laserlight View Post
    What is your idea and what have you tried?
    I tried this :

    Code:
    #include <stdio.h>
    
    
    int main()
    {
            int x; 
        
    
        printf("Enter positive number: ");
        scanf("%d",&x);
        
    
        
        printf("%d ",x<<2+x<<4+x<<6+x<<8  );
        
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're missing the "solved with the use of bitwise operators" part of "solved with the use of bitwise operators and bit shiftings".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    what do you mean? I have to do somewhere bit shiftings or add operators ? I also tried this ... :


    Code:
    #include <stdio.h>
    
    
    int main()
    {
    		int x; 
    	
    	printf("Enter positive number: ");
    	scanf("%d",&x);
    	
    	printf("%d ",x<<2&x<<4&x<<6&x<<8  );
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you try to print the value of just one digit and see what you get?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    I tried it and it gives me again 0 .. I 'm assuming it is 0 + 0 + 0 + 0 + 0 = 0

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you try and what was your input?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    I also tried that but it gives me 12 as an outcome for the integer 170:

    Code:
    #include <stdio.h>
    
    
    
    
    int main(void)
    {
            int x; 
        
    
    
        printf("Enter positive number: ");
        scanf("%d",&x);
        
        
        printf("%d ",(((1 << 8) - 1) & (x >> (8 - 1)))+(((1 << 6) - 1) & (x >> (6 - 1)))+(((1 << 4) - 1) & (x >> (4 - 1)))+(((1 << 2) - 1) & (x >> (2 - 1))));
        
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do, or do not. There is no try.

    Okay that's not really true here. It is okay to try, but there must be some reasoning behind what you're trying. So, how would you get the bit at position 2?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    can you help .. ?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Start with analysing
    int a = x<<2;
    printf("a=%d, set=%d\n", a, a != 0);

    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.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Code:
    #include <stdio.h>
    
    int main()
    {
        unsigned char aByte = 1;     // == 00000001
        unsigned char myInput = 126; // just some random value which fits inside a byte (unsigned char)
        unsigned char myMask =  170; // 10101010
    
        int cntr = 0;
    
        // after each loop the bit of aByte is shifted 1 position to the left. From the start, which is position 0
        // after (!) 7 times shifted to the left, the bit is shifted outside aByte and aByte has value == 0
    
        for(; aByte; aByte <<= 1)
            if(aByte & myMask)    // test whether the current position of the bit in aByte matches with one of the mask-bits
                if(... find out yourself ...) // if so, test whether the current position of aByte also matches with your input
                    ++cntr;
    
        printf("%d matching bits counted\n", cntr);
        return(1);
    }

  15. #15
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    @ddutch, Returning 1 at the end of main is an extremely unportable way to indicate "success" (if that's what you are trying to do). You should return 0 or EXIT_SUCCESS (defined in stdlib.h; most likely defined as 0).

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