Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
j=0;
for(i=0 ; i < 8 ; i ++)
if(((DATA>>i)&1) == 1) j++;
You need to check every bit, then if bit high, increment counter.
Code:j=0; for(i=0 ; i < 8 ; i ++) if(((DATA>>i)&1) == 1) j++;
void main()
{
port_B =portb;
DATA = port_B;
while (1) {
if (UART1_Data_Ready()) {
j=0;
for(i=0 ; i < 8 ; i ++)
{
if(((DATA>>i)&1) == 1) j++;
}
m=Int2Str(j);
UART1_Write(m);
}
}
}
UART1_Write(Lo(J));
UART1_Write(Hi(J));
How can we take the number of "high"s in a binary number ?
like
0b10100000 -> 2
0b01000101 -> 3
0b01111000 -> 4
while((x) & (x-1))
{
count++;
x = x-1;
}