[DOC] add gmp_testbit() function

tests if the specified bit is set and returns false/true
This commit is contained in:
Antony Dovgal 2007-04-23 14:57:37 +00:00
parent 84827aabc4
commit 2bdfafadeb
4 changed files with 117 additions and 0 deletions

1
NEWS
View File

@ -57,6 +57,7 @@ PHP NEWS
set params during context creation. (Sara)
. "context" and "binary_pipes" params in "other_options" arg. (Sara)
. stream_resolve_include_path(). (Sara)
- Added gmp_testbit() function. (Tony)
- Added shm_has_var() function. (Mike)
- Added str_getcsv() function. (Sara)
- Added ext/hash support to ext/session's ID generator. (Sara)

View File

@ -242,6 +242,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_gmp_clrbit, 0)
ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO()
static
ZEND_BEGIN_ARG_INFO(arginfo_gmp_testbit, 0)
ZEND_ARG_INFO(0, a)
ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO()
static
ZEND_BEGIN_ARG_INFO(arginfo_gmp_popcount, 0)
ZEND_ARG_INFO(0, a)
@ -308,6 +314,7 @@ zend_function_entry gmp_functions[] = {
ZEND_FE(gmp_xor, arginfo_gmp_xor)
ZEND_FE(gmp_setbit, arginfo_gmp_setbit)
ZEND_FE(gmp_clrbit, arginfo_gmp_clrbit)
ZEND_FE(gmp_testbit, arginfo_gmp_testbit)
ZEND_FE(gmp_scan0, arginfo_gmp_scan0)
ZEND_FE(gmp_scan1, arginfo_gmp_scan1)
ZEND_FE(gmp_popcount, arginfo_gmp_popcount)
@ -1531,6 +1538,35 @@ ZEND_FUNCTION(gmp_clrbit)
}
/* }}} */
/* {{{ proto bool gmp_testbit(resource a, int index) U
Tests if bit is set in a */
ZEND_FUNCTION(gmp_testbit)
{
zval **a_arg, **ind_arg;
int index;
mpz_t *gmpnum_a;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &a_arg, &ind_arg) == FAILURE){
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(gmpnum_a, mpz_t *, a_arg, -1, GMP_RESOURCE_NAME, le_gmp);
convert_to_long_ex(ind_arg);
index = Z_LVAL_PP(ind_arg);
if (index < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
RETURN_FALSE;
}
if (mpz_tstbit(*gmpnum_a, index)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto int gmp_popcount(resource a) U
Calculates the population count of a */
ZEND_FUNCTION(gmp_popcount)

View File

@ -70,6 +70,7 @@ ZEND_FUNCTION(gmp_xor);
ZEND_FUNCTION(gmp_random);
ZEND_FUNCTION(gmp_setbit);
ZEND_FUNCTION(gmp_clrbit);
ZEND_FUNCTION(gmp_testbit);
ZEND_FUNCTION(gmp_scan0);
ZEND_FUNCTION(gmp_scan1);
ZEND_FUNCTION(gmp_popcount);

79
ext/gmp/tests/039.phpt Normal file
View File

@ -0,0 +1,79 @@
--TEST--
gmp_testbit() basic tests
--FILE--
<?php
$n = gmp_init(0);
var_dump(gmp_testbit($n, -10));
var_dump(gmp_testbit($n, 0));
var_dump(gmp_testbit($n, 1));
var_dump(gmp_testbit($n, 100));
$n = gmp_init(-1);
var_dump(gmp_testbit($n, 1));
var_dump(gmp_testbit($n, -1));
$n = gmp_init("1000000");
var_dump(gmp_testbit($n, 1));
gmp_setbit($n, 1);
var_dump(gmp_testbit($n, 1));
var_dump(gmp_strval($n));
gmp_setbit($n, 5);
var_dump(gmp_testbit($n, 5));
var_dump(gmp_strval($n));
$n = gmp_init("238462734628347239571823641234");
var_dump(gmp_testbit($n, 5));
gmp_setbit($n, 5);
var_dump(gmp_testbit($n, 5));
var_dump(gmp_strval($n));
gmp_clrbit($n, 5);
var_dump(gmp_testbit($n, 5));
var_dump(gmp_strval($n));
echo "Done\n";
?>
--EXPECTF--
Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d
bool(false)
bool(false)
bool(true)
string(7) "1000002"
bool(true)
string(7) "1000034"
bool(false)
bool(true)
string(30) "238462734628347239571823641266"
bool(false)
string(30) "238462734628347239571823641234"
Done
--UEXPECTF--
Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d
bool(false)
bool(false)
bool(true)
unicode(7) "1000002"
bool(true)
unicode(7) "1000034"
bool(false)
bool(true)
unicode(30) "238462734628347239571823641266"
bool(false)
unicode(30) "238462734628347239571823641234"
Done