在 Julia 中计算数字的二进制表示中的尾随 1 和 0 – trailing_ones() 和 trailing_zeros() 方法
trailing_ones()
是 julia 中的一个内置函数,用于查找尾随指定数字的二进制表示形式的个数。
Syntax: trailing_ones(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the number of ones trailing the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of trailing_ones() method
# Getting the number of ones trailing
# the binary representation of
# the specified number.
println(trailing_ones(2))
println(trailing_ones(15))
println(trailing_ones(13))
println(trailing_ones(3))
输出:
0
4
1
2
trailing_zeros()
trailing_zeros()
是 julia 中的内置函数,用于查找指定数字的二进制表示后面的零数。
Syntax: trailing_zeros(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the number of zeros trailing the binary representation of the specified number.
例子:
# Julia program to illustrate
# the use of trailing_zeros() method
# Getting the number of zeros trailing
# the binary representation of
# the specified number.
println(trailing_zeros(2))
println(trailing_zeros(56))
println(trailing_zeros(100))
println(trailing_zeros(32))
输出:
1
3
2
5