]> granicus.if.org Git - php/commitdiff
random_int: Fix power of two check.
authorLauri Kenttä <lauri.kentta@gmail.com>
Sun, 10 May 2015 10:40:29 +0000 (13:40 +0300)
committerNikita Popov <nikic@php.net>
Sun, 10 May 2015 11:00:45 +0000 (13:00 +0200)
(x & ~x) is always 0.
((x & (~x + 1)) != x) works.
((x & (x - 1)) != 0) works too.

ext/standard/random.c

index 12c25031d83a446585b9b50fefdef30df58bc3be..4a1adbfb0263043371344c2a23fcfd1a30d2520e 100644 (file)
@@ -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;