Re: [PATCH] random_int: Fix power of two check.

From: Date: Sun, 10 May 2015 11:03:06 +0000
Subject: Re: [PATCH] random_int: Fix power of two check.
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-86149@lists.php.net to get a copy of this message
On Sun, May 10, 2015 at 12:40 PM, Lauri Kenttä <lauri.kentta@gmail.com> wrote: > (x & ~x) is always 0. > ((x & (~x + 1)) != x) works. > ((x & (x - 1)) != 0) works too. > --- > ext/standard/random.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/ext/standard/random.c b/ext/standard/random.c > index 12c2503..4a1adbf 100644 > --- a/ext/standard/random.c > +++ b/ext/standard/random.c > @@ -182,7 +182,7 @@ PHP_FUNCTION(random_int) > umax++; > > /* Powers of two are not biased */ > - if ((umax & ~umax) != umax) { > + if ((umax & (umax - 1)) != 0) { > /* Ceiling under which ZEND_LONG_MAX % max == 0 */ > zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % > umax) - 1; > > -- > 2.4.0 > Good catch, thanks! Applied via https://github.com/php/php-src/commit/cf7e5357a46afe1dca978f4887bbd83a7507ce69 . Nikita

« previous php.internals (#86149) next »