Re: algorithm to identify number from 0-16384 having at most 4 1s in its binary number



Yes I need the numbers with at the most 4 1s or less than that. so i
also need numbers with 3 1s, 2 1s and 1 1s.


i didn't really get what Erik Naggum suggested. But I worked out the
following way:


n = 2^14
count = 0

for ( i=0;i< n;i++)
{
for (j=0;j<n;j=j*2)
{
if (i AND j) = j
count = count + 1
}
if (count <=4)
print i
}

This will print all the numbers having at most 4 1s in its equivalent
binary number.

What do you think of this??

.