Count Set Bits

Count the number of set bits (1s) in a number's binary representation.

Code

General
n.to_s(2).count('1')

Parameters

Number

Server

More Ruby Snippets