Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ public function equals($x) {
public function sign() {
return $this->value[0] === "-" ? -1 : ($this->value === "0" ? 0 : 1);
}

public function shiftLeft($n) {
return $this->mul((new static(2))->pow($n));
}

public function shiftRight($n) {
$newInt = $this->div((new static(2))->pow($n));

if ($newInt->add($n)->cmp(0) < 0) {
return $newInt->sub(1);
}

return $newInt;
}
}

}
Expand Down
2 changes: 2 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function testOp() {
testB((new BigInteger(20))->binaryAnd(18), "16", "binaryAnd");
testB((new BigInteger(20))->binaryOr(18), "22", "binaryOr");
testB((new BigInteger(20))->binaryXor(18), "6", "binaryXor");
testB((new BigInteger(3))->shiftLeft(4), "48", "shiftLeft");
testB((new BigInteger(3))->shiftRight(4), "0", "shiftRight");
testB((new BigInteger(20))->setbit(3), "28", "setbit");
test((new BigInteger(20))->testbit(4), true, "testbit true");
test((new BigInteger(20))->testbit(3), false, "testbit false");
Expand Down