Skip to content

Core\BigInt::shiftedRight

$bigInt->shiftedRight(uint $bits): Core\BigInt$bigInt->shiftedRight(
    uint $bits,
): Core\BigInt

Shifts a Core\BigInt to the right.

shiftedRight moves every bit of the number $bits places to the right. The bits that fall off the right end are gone. That is the same as dividing the number by two, $bits times, and rounding down.

Rounding down means rounding toward negative infinity, not toward zero. 7 shifted by 1 place is 3, and -7 shifted by 1 place is -4.

You may shift by more places than the number is wide. A positive number then gives 0, and a negative number gives -1. Shifting by 0 places gives the number back.

shiftedRight never throws an error. The result is never wider than the number you started with, so it never reaches the width limit.

shiftedRight is a method because >> does not work on an object. To shift the other way, use Core\BigInt::shiftedLeft.

Parameters

  • $bitsuintHow many bits to shift by. If it is larger than the width of this value, the result is 0 or -1.

Return value

Returns Core\BigInt. A new BigInt, with the bits of this value moved $bits places to the right.

Changelog

VersionChange
0.0.6Each Core\BigInt method that computes a new number is named for its result: plus, minus, multipliedBy, dividedBy, remainder, raisedTo, raisedToModulo, negated, shiftedLeft and shiftedRight. The old names were add, sub, mul, div, mod, pow, powMod, neg, shl and shr.

Examples

Shifting a number to the right

<?nvs
// Shows how to shift a whole number to the right. Each place you shift by halves it.
// `shiftedRight` moves the bits 1 place to the right, which halves the number. This
// prints 3.
echo Core\BigInt::of(6)->shiftedRight(1), "\n";
// The bits that fall off the right end are gone, so 7 halved is 3. These two
// lines print 3 and 1.
echo Core\BigInt::of(7)->shiftedRight(1), "\n";
echo Core\BigInt::of(255)->shiftedRight(7), "\n";
// Shifting by 0 places gives the number back. This prints 9.
echo Core\BigInt::of(9)->shiftedRight(0), "\n";
// A number far larger than an `int` can store is shifted the same way. This
// prints 1.
echo Core\BigInt::of(1)->shiftedLeft(64)->shiftedRight(64), "\n";
Output
3
3
1
9
1

Negative numbers and very large shifts

<?nvs
// Shows what `shiftedRight` does with a negative number, and with more places than the
// number is wide.
// Shifting right rounds down, toward negative infinity. -7 halved is -3.5, and
// rounding down gives -4. This prints -4.
echo Core\BigInt::of(-7)->shiftedRight(1), "\n";
// The same number the other way round: 7 halved is 3.5, and rounding down
// gives 3. This prints 3.
echo Core\BigInt::of(7)->shiftedRight(1), "\n";
// You may shift by more places than the number is wide. A positive number
// gives 0. This prints 0.
echo Core\BigInt::of(255)->shiftedRight(1000), "\n";
// A negative number gives -1, because rounding down never reaches 0 from
// below. This prints -1.
echo Core\BigInt::of(-255)->shiftedRight(1000), "\n";
Output
-4
3
0
-1

Multiplying two fixed point prices

<?nvs
// Shows a program multiplying two fixed-point numbers. A price is stored as a
// whole number with 64 bits after the point. The product of two of them has
// 128 bits after the point, so it is shifted right by 64 to get back.
class Fixed
{
// Returns $whole as a value with 64 bits after the point.
public static function of(int $whole): Core\BigInt
{
return Core\BigInt::of($whole)->shiftedLeft(64);
}
// Returns the product of two such values, at the same scale again.
public static function mul(Core\BigInt $left, Core\BigInt $right): Core\BigInt
{
return $left->multipliedBy($right)->shiftedRight(64);
}
// Returns the whole part of such a value.
public static function whole(Core\BigInt $value): Core\BigInt
{
return $value->shiftedRight(64);
}
}
// 3 times 5 is 15, and the result is stored at the same scale. This prints 15.
echo Fixed::whole(Fixed::mul(Fixed::of(3), Fixed::of(5))), "\n";
// A half is 1 shifted 63 places to the left. 7 times a half is 3.5, and the
// whole part of that is 3. This prints 3.
var $half = Core\BigInt::of(1)->shiftedLeft(63);
echo Fixed::whole(Fixed::mul(Fixed::of(7), $half)), "\n";
Output
15
3