#include <stdio.h>
int main() {
char data = 0x21; //0010 0001 //char or int same
int count;
int i = 2;
//while doing bellow copy tha data value for further use
int cp = data;
while(data!=0){//till 0000 0000
i++;
if(data & 1){ ///The value 1 in binary representation is 00000001.
count++;
printf("%d\n",i);
}
data = data >> 1;
}
/* ex: 0001 0001
Iteration 1: Current value of 'data': 0x21 (0001 0001)
Iteration 2: Current value of 'data': 0x10 (0000 1000)
Iteration 3: Current value of 'data': 0x08 (0000 0100)
Iteration 4: Current value of 'data': 0x04 (0000 0010)
Iteration 5: Current value of 'data': 0x02 (0000 0001)
*/
printf("data = %d cp = %X\n", data, cp);
printf("%d %d",count, i);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: