在 Julia 中计算二进制表示中数字的 1 和 0 – count_ones() 和 count_zeros() 方法
count_ones()
是 julia 中的一个内置函数,用于计算指定数字的二进制表示中存在的个数。
Syntax: count_ones(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the calculated number of ones present in the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of count_ones() method
# Getting number of ones present
# in the binary representation
# of the specified number.
println(count_ones(0))
println(count_ones(1))
println(count_ones(15))
println(count_ones(20))
输出:
0
1
4
2
count_zeros
count_zeros()
是 julia 中的内置函数,用于计算指定数字的二进制表示中存在的零的数量。
Syntax: count_zeros(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the calculated number of zeros present in the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of count_zeros() method
# Getting number of zeros present
# in the binary representation
# of the specified number.
println(count_zeros(0))
println(count_zeros(1))
println(count_zeros(Int32(2 ^ 16 - 1)))
println(count_zeros(Int32(2 ^ 16 - 10)))
输出:
64
63
16
18