Skip to content
Related Articles

Related Articles

Improve Article

Perl | Math::BigInt->brsft() method

  • Last Updated : 13 Sep, 2021

Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators.
brsft() method of Math::BigInt module is used to right shift the given value by a base given as parameter.
 

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

Note: By default it takes 2 as a base.
Example 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);
Output: 
12
23

 

There is an exception with the negative value and that is only with the base 2, the output will be the difference between the actual output and the original number. Following example will make it clear:
Example 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);
Output: 
15
-16

 

My Personal Notes arrow_drop_up