Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Binary number conversion

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
How can we take the number of "high"s in a binary number ?

like

0b10100000 -> 2
0b01000101 -> 3
0b01111000 -> 4
 

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++;
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
srhoar,
Very nice links, I didnt got any links like this before
 

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++;

what can be the problem with the bel

Code:
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);



    }

     }

}



used

Code:
        UART1_Write(Lo(J));
       UART1_Write(Hi(J));

too, but no luck
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top