core :: Int :: number_bits
self
assert 10.number_bits(1) == 2
assert 10.number_bits(0) == 30
# Returns the number of bits of specified value (0 or 1) in `self`
#
# assert 10.number_bits(1) == 2
# assert 10.number_bits(0) == 30
fun number_bits(value: Int): Int `{
assert(value == 0 || value == 1);
long int bound = 1L << 31;
int count = 0;
long int i;
if(value == 1)
{
for(i=bound; i>0; i/=2)
{
if(self & i)
count++;
}
}
else
{
for(i=bound; i>0; i/=2)
{
if(!(self & i))
count++;
}
}
return count;
`}
lib/core/bitset.nit:72,2--100,3