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




"Ujjaval" <ujjaval@xxxxxxxxx> wrote in message
news:1138059463.344917.43680@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hey all,
>
> I want to get the numbers from 0-16384 whose equivalent binary number
> has at the most 4 1s in it.
>
> I can do this by setting up a counter and doing an AND operation of a
> number with all numbers which are power of 2 between 0-16384. I update
> the couter for each successful AND operation and at the end check
> whether the counter is <= 4 or not.
>
> Is there any more efficient way to do this??
>
> Thanks,
> Ujjaval

Question is unclear "has at the most 4 1s in it"
It that only numbers with 4 ones in it ?
does that include numbers with only 3 ones in it?
( 2 ones in it?)

the rest is easy


.



Relevant Pages