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

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 shifing

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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);

chevron_right


Output:

15
-16
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.