Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Num Ones

def count_bits(mut num: Int) -> Int:
	result = 0
	while num:
		result += num % 2
		num = num >> 1
	return result

def count_bits2(mut num: Int) -> Int:
	result = 0
	while num:
		result += num & 1
		num >>= 1
	return result

from std.testing import assert_equal

def main() raises:
	num = 11
	assert_equal(3, count_bits(num))
	num = 128
	assert_equal(1, count_bits(num))
	num = 2147483645
	assert_equal(30, count_bits(num))

	num = 11
	assert_equal(3, count_bits2(num))
	num = 128
	assert_equal(1, count_bits2(num))
	num = 2147483645
	assert_equal(30, count_bits2(num))

View source on GitHub