珀尔 |数学::BigInt->brsft() 方法
Perl 中的Math::BigInt模块提供了表示具有任意精度和重载算术运算运算符的整数的对象。
Math::BigInt模块的brsft()方法用于将给定值右移一个作为参数给出的基数。
Syntax: Math::BigInt->brsft()
Parameter:
$y- number of times shifting is to be done
$n- base of shifting
Returns: a result by dividing the number by the base by given number of times
注意:默认情况下,它以 2 为基数。
示例 1:
perl
#!/usr/bin/perl
# Import Math::BigInt module
use Math::BigInt;
# Right shifting one time with base 2
$x = Math::BigInt->new(25);
$x->brsft(1); # same as $x >> 1
print("$x\n");
# Right shifting 2 times with base 10
$x = Math::BigInt->new(2345);
$x->brsft(2, 10);
print($x);
perl
#!/usr/bin/perl
# Import Math::BigInt module
use Math::BigInt;
# Right shifting one time with base 2
$x = Math::BigInt->new(31);
$x->brsft(1); # same as $x >> 1
print("$x\n");
# Right shifting the negative value
$x = Math::BigInt->new(-31);
$x->brsft(1);
print($x);
输出:
12
23
负值有一个例外,即仅以 2 为底,输出将是实际输出与原始数字之间的差异。以下示例将清楚地说明:
示例 2:
perl
#!/usr/bin/perl
# Import Math::BigInt module
use Math::BigInt;
# Right shifting one time with base 2
$x = Math::BigInt->new(31);
$x->brsft(1); # same as $x >> 1
print("$x\n");
# Right shifting the negative value
$x = Math::BigInt->new(-31);
$x->brsft(1);
print($x);
输出:
15
-16