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.

Numder of ones in a binary number

Status
Not open for further replies.

Jesinth

Newbie level 4
Joined
Mar 21, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Chennai, India
Activity points
1,328
Is there any way to find out the number of ones in the binary representation of
(3 x 512) + (7 x 64) + (5 x 8 ) + 3 without actual calculation of decimal equicalent and converting into binary.

(The resultant decimal is 2027 with binary equivalent 11111101011 which has 9 ones.)

How to get this value 9 from the expression above?
 

Jesinth,
Take the result and divide it by powers of 2 starting with the highest power that produces biggest divisible number. Then decrease the power each time you divide, the result should be a 1 or a 0. In essence you are converting the number to binary, but you can just count the number of 1's as you loop though the 2^x.

Hope that helps some.
Sckoarn
 

Yes, there IS a way for you to do that, WITHOUT calculating the result.

The clue is in the fact that 512, 64 and 8 can each be represented by a single bit.
However, we don't need to worry about the actual bit-position these occupy in the result, because they are going to be multiplied (in this case, by 3, 7 and 5).
So all you need to know is how many bits each of those multipliers require in their binary representation.
3 requires 2 "1" bits
7 requires 3 "1" bits
5 requires 2 "1" bits (that's 7 bits so far)
Then there's the last term, 3, which in itself requires 2 "1" bits in its representation. So that's a total of 9 "1" bits.

I should warn you that this is probably some text book example you've presented to us, and is simpler than most real-life situations. Even if you were always presented with power-of-two factors (in this case 512, 64 and 8 ) which conveniently are represented by a single bit in binary notation, there is the risk that the multiplier might increase the value to a magnitude which reaches or exceeds one of the other terms. In the example, 7 x 64 = 448 and 448 is less than 512 so they don't conflict.
But try using this technique if the middle term was (9 x 64). Or if the last term was + 77. These should illustrate the hazards.
 
Last edited:

The simplest solution in this case is
(3 x 512) + (7 x 64) + (5 x 8 ) + 3
= (3 x 8^3) + (7 x 8^2) + (5 x 8 ) + 3
= 3753 (octal)

3 in octal has 2 hot bits, 7 has 3 hot bits, and so on.
Total number of hot bits is 9.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top