Ruby Integer bit_length函数与示例
Ruby 中的bit_length函数返回 int 值的位数。这里的位数表示与符号位不同的最高位的位位置。如果没有这样的位,则返回零。
Syntax: number.bit_length
Parameter: The function takes the number and count its number of bits
Return Value: The function returns number of bits in the value of int.
示例 #1:
# Ruby program of Integer bit_length function
# Initializing the numbers
num1 = 20
num2 = 8
num3 = 0
num4 = -1
# Prints the bit length
puts num1.bit_length
puts num2.bit_length
puts num3.bit_length
puts num4.bit_length
输出 :
5
4
0
0
示例 #2:
# Ruby program of Integer bit_length function
# Initializing the numbers
num1 = 2**1000
num2 = -100
num3 = 0
num4 = 2**3
# Prints the bit length
puts num1.bit_length
puts num2.bit_length
puts num3.bit_length
puts num4.bit_length
输出 :
1001
7
0
4