你的朋友 Eliud 從奶奶 Tigist 那裡繼承了一座農場。 她的奶奶是一位發明家,總是把東西做得過度複雜。 雞舍裡有一個數字顯示器,上面顯示一個編碼過的數字,代表所有可能撿到的蛋的位置。
Eliud 請你寫一個程式,顯示雞舍裡實際的蛋的數量。
位置資訊的編碼方式計算如下:
1,空的位置就記下0。範例 1:
Chicken Coop:
_ _ _ _ _ _ _
|E| |E|E| | |E|
Resulting Binary:
1 0 1 1 0 0 1
Decimal number on the display:
89
Actual eggs in the coop:
4
範例 2:
Chicken Coop:
_ _ _ _ _ _ _ _
| | | |E| | | | |
Resulting Binary:
0 0 0 1 0 0 0 0
Decimal number on the display:
16
Actual eggs in the coop:
1
你的任務是計算一個數字的二進位表示法中有多少個 1 位元。
不要使用標準函式庫提供的位元計數功能!請改用其他基本工具,靠自己解決這個問題。