Skip to content

Core\BigInt::shiftedLeft

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

Shifts a Core\BigInt to the left.

shiftedLeft moves every bit of the number $bits places to the left. That is the same as multiplying the number by two, $bits times. 1 shifted by 10 places is 1024. Shifting by 0 places gives the number back.

The sign stays what it was. A negative number stays negative, so -3 shifted by 4 places is -48.

Every place makes the number one bit wider. One call may produce a number of at most 1048576 bits, and a wider answer throws an ArithmeticError. The width you start with counts toward that bound. shiftedLeft tests the width before it shifts, so the large number is never built and no memory is used for it.

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

Parameters

  • $bitsuintHow many bits to shift by.

Return value

Returns Core\BigInt. A new BigInt. Unless this value is 0, it is $bits bits wider than this value.

Errors

  • ArithmeticErrorThe result would be wider than 1 048 576 bits.

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 left

<?nvs
// Shows how to shift a whole number to the left. Every place doubles it.
// `shiftedLeft` moves the bits 10 places to the left, which multiplies the number by
// 1024. This prints 1024.
echo Core\BigInt::of(1)->shiftedLeft(10), "\n";
// The answer may be far larger than an `int` can store. This prints
// 18446744073709551616.
echo Core\BigInt::of(1)->shiftedLeft(64), "\n";
// Shifting by 0 places gives the number back. This prints 3.
echo Core\BigInt::of(3)->shiftedLeft(0), "\n";
// The sign stays what it was. This prints -48.
echo Core\BigInt::of(-3)->shiftedLeft(4), "\n";
Output
1024
18446744073709551616
3
-48

How far a number may be shifted

<?nvs
// Shows how far a number may be shifted. One call may produce a number of at
// most 1048576 bits.
// 1 shifted by 1048575 places is exactly 1048576 bits wide, which is allowed.
// That number has 315653 digits in base 10.
var $widest = Core\BigInt::of(1)->shiftedLeft(1048575);
echo Core\Str::length($widest->toString()), "\n";
// One place more is too wide. `shiftedLeft` throws an `ArithmeticError`, and the large
// number is never built. This prints "too wide".
try {
echo Core\BigInt::of(1)->shiftedLeft(1048576), "\n";
} catch (ArithmeticError $tooWide) {
echo "too wide\n";
}
// The number you start with counts toward the width. This one is 100 bits
// wide, so 1048476 places are allowed and 1048477 are not.
var $wide = Core\BigInt::of(1)->shiftedLeft(99);
echo $wide->shiftedLeft(1048476)->sign(), "\n";
try {
echo $wide->shiftedLeft(1048477), "\n";
} catch (ArithmeticError $alsoTooWide) {
echo "too wide\n";
}
Output
315653
too wide
1
too wide

Building the number of an ipv6 address

<?nvs
// Shows a program building the number of an IPv6 address. Such an address is
// 128 bits wide: a network prefix of 64 bits, and an interface part of 64 bits
// under it.
class Address
{
// Returns the two halves of an address as one number.
public static function combine(Core\BigInt $prefix, Core\BigInt $iface): Core\BigInt
{
return $prefix->shiftedLeft(64)->plus($iface);
}
}
var $prefix = Core\BigInt::parse('20010db8000000ff', { radix: 16 });
// `shiftedLeft(64)` moves the prefix above the interface part. This prints
// 20010db8000000ff0000000000000042.
var $iface = Core\BigInt::parse('42', { radix: 16 });
echo Address::combine($prefix, $iface)->format({ radix: 16 }), "\n";
// The same prefix with another interface part. This prints
// 20010db8000000ff0000000000000043.
var $other = Core\BigInt::parse('43', { radix: 16 });
echo Address::combine($prefix, $other)->format({ radix: 16 }), "\n";
Output
20010db8000000ff0000000000000042
20010db8000000ff0000000000000043