Created
February 13, 2019 19:50
-
-
Save venelrene/b02ca79e43eab8bdf1655a9f0e49c5bd to your computer and use it in GitHub Desktop.
Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative. Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_bits(n) | |
n.to_s(2).scan("1").count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment