Count Set Bits

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

Code

General
echo "obase=2; $n" | bc | tr -cd '1' | wc -c

Parameters

Number

Server

More Bash Snippets