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

1. ## 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. What is your idea and what have you tried?

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

Originally Posted by laserlight
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  );

}```

4. You're missing the "solved with the use of bitwise operators" part of "solved with the use of bitwise operators and bit shiftings".

5. 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  );
}```

6. Why don't you try to print the value of just one digit and see what you get?

7. I tried it and it gives me again 0 .. I 'm assuming it is 0 + 0 + 0 + 0 + 0 = 0

8. What did you try and what was your input?

9. 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))));

}```

10. 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?

11. can you help .. ?

int a = x<<2;
printf("a=%d, set=%d\n", a, a != 0);

13. 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);
}```

14. @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).