在 Julia 中计算数字的二进制表示中的前导 1 和 0 –leading_ones() 和leading_zeros() 方法
leading_ones()
是 julia 中的一个内置函数,用于查找在指定数字的二进制表示中领先的个数。
Syntax: leading_ones(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the number of ones leading the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of leading_ones() method
# Getting the number of ones leading
# the binary representation of
# the specified number.
println(leading_ones(UInt16(2 ^ 16 - 1)))
println(leading_ones(UInt16(2 ^ 16 - 2)))
println(leading_ones(UInt32(2 ^ 32 - 1)))
println(leading_ones(UInt32(2 ^ 32 - 2)))
输出:
16
15
32
31
前导零()
leading_zeros()
是 julia 中的一个内置函数,用于查找在指定数字的二进制表示前导零的数量。
Syntax: leading_zeros(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the number of zeros leading the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of leading_zeros() method
# Getting the number of zeros leading
# the binary representation of
# the specified number.
println(leading_zeros(Int16(5)))
println(leading_zeros(Int16(15)))
println(leading_zeros(Int32(30)))
println(leading_zeros(Int32(36)))
输出:
13
12
27
26