[algogeeks] Re: 1's counting

2010-08-09 Thread Avik Mitra


Thanks Dave

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 1's counting

2010-08-06 Thread bittu
.

 Is there a quick way to count the number of set bits in this number
 manually???

converting that no. in to binary as

int bin(int n)
{

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 1's counting

2010-08-06 Thread Dave
@Avik: Correcting/augmenting your post:

N = (3*4096+15*256+3*16+3)
   = 3*(2^12) + 15*(2^8) + 3*(2^4) + 3*(2^0)
   = (2+1)*(2^12) + (8+4+2+1)*(2^8) + (2+1)*(2^4) + (2+1)
   = (2^1+2^0)*(2^12) + (2^3+2^2+2^1+2^0)*(2^8) + (2^1+2^0)*(2^4) +
(2^1+2^0)
   = 2^13 + 2^12 + 2^11 + 2^10 + 2^9 + 2^8 + 2^5 + 2^4+ 2^1 + 2^0

So there are 10 1's in the binary representation of the number N.

Dave

On Aug 6, 12:42 am, Avik Mitra tutai...@gmail.com wrote:
 N = (3*4096+15*256+3*16+3)
    = 3* (2^10) + 15*( 2^8) + 3*(2^4) + 3* (2^0)
    = (1+2)*(2^10) + (1+2+2^2+ 2^3)*(2^8) + (1+2)*(2^4) + (1+2)
    = (2^10 + 2^11) + (2^8+2^9+2^10+2^11) + (2^4 + 2^6)+ (1+2)
    = 2^11+2^12+2^8+2^9+2^4+2^6+2+1
    = 1 + 2 + 2^4 + 2^6 + 2^8 + 2^9 + 2^11 + 2^12

 So there are 8 1's in the binary representation of the number N.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 1's counting

2010-08-05 Thread Avik Mitra



N = (3*4096+15*256+3*16+3)
   = 3* (2^10) + 15*( 2^8) + 3*(2^4) + 3* (2^0)
   = (1+2)*(2^10) + (1+2+2^2+ 2^3)*(2^8) + (1+2)*(2^4) + (1+2)
   = (2^10 + 2^11) + (2^8+2^9+2^10+2^11) + (2^4 + 2^6)+ (1+2)
   = 2^11+2^12+2^8+2^9+2^4+2^6+2+1
   = 1 + 2 + 2^4 + 2^6 + 2^8 + 2^9 + 2^11 + 2^12

So there are 8 1's in the binary representation of the number N.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 1's counting

2010-08-04 Thread Dave
The straightforward way is simply to write the number out in binary
and count the one-bits. It works for any number.

In this specific case, since the multiplications are by powers of 2,
resulting in shifts, and the resulting binary numbers don't overlap,
the number of bits is
bitcount(3*4096) + bitcount(15*256) + bitcount(3*16) + bitcount(3)
= bitcount(3) + bitcount(15) + bitcount(3) + bitcount(3)
= 2 + 4 + 2 + 2
= 10.

Dave

On Aug 3, 2:04 pm, amit amitjaspal...@gmail.com wrote:
 (3*4096+15*256+3*16+3). How many 1's are there in the binary
 representation of the result.

 Is there a quick way to count the number of set bits in this number
 manually???

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.