PHP: gmp_testbit - Manual (original) (raw)
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
gmp_testbit — Tests if a bit is set
Description
Parameters
num
A GMP object, an int, or a string that can be interpreted as a number following the same logic as if the string was used in gmp_init() with automatic base detection (i.e. when base is equal to 0).
index
The bit to test
Return Values
Returns [true](reserved.constants.php#constant.true) if the bit is set in num, otherwise [false](reserved.constants.php#constant.false).
Errors/Exceptions
An [E_WARNING](errorfunc.constants.php#constant.e-warning) level error is issued when index is less than zero, and [false](reserved.constants.php#constant.false) is returned.
Examples
Example #1 gmp_testbit() example
<?php
$n = gmp_init("1000000");
var_dump(gmp_testbit($n, 1));
gmp_setbit($n, 1);
var_dump(gmp_testbit($n, 1));
?>The above example will output:
Found A Problem?
18 years ago
gmp_testbit will be very useful for my (over 64-bit) bitmask checking.
In the meantime, I think these are the best replacements.
Mostly full:
<?php
(gmp_scan1($a, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">index) == </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">==</span></span></span></span>index)
?>
Mostly empty:
<?php
(gmp_scan0($a, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mo stretchy="false">)</mo><mo stretchy="false">!</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">index) != </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mclose">)!</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>index)
?>
Still worst-case O(N), though.