TheAlgorithms-Ruby/bit_manipulation/binary_count_setbits.rb
Vitor Oliveira 8bb781f790 Minor fixes
2021-09-03 13:24:58 -07:00

26 lines
411 B
Ruby

def binary_count_setbits(x)
raise 'Input must be a positive integer' if x < 0
binary = x.to_s(2)
binary.chars.map { |c| c.to_i }.reduce(:+)
end
begin
binary_count_setbits(-1)
rescue StandardError => e
puts e.message
end
# Input must be a positive integer
puts binary_count_setbits(0)
# 0
puts binary_count_setbits(1)
# 1
puts binary_count_setbits(1024)
# 1
puts binary_count_setbits(1023)
# 10