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)
C Read an integer and count 1s only at positions 2, 4, 6 , 8 of the binary
Quote:
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 );
}