BinaryCountSetBits [二进制串数1]
# 介绍
计算数字的二进制表示中 1 的个数。
# 实现
# JavaScript
/* This script will find number of 1's in binary representation of given number */
function BinaryCountSetBits (a) {
// convert number into binary representation and return number of set bits in binary representation
return a.toString(2).split('1').length - 1
}
1
2
3
4
5
2
3
4
5
编辑 (opens new window)
上次更新: 2022/10/20, 21:49:42