Skip to content

Commit cf7e535

Browse files
Metabolixnikic
authored andcommitted
random_int: Fix power of two check.
(x & ~x) is always 0. ((x & (~x + 1)) != x) works. ((x & (x - 1)) != 0) works too.
1 parent 8c8889e commit cf7e535

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

ext/standard/random.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PHP_FUNCTION(random_int)
182182
umax++;
183183

184184
/* Powers of two are not biased */
185-
if ((umax & ~umax) != umax) {
185+
if ((umax & (umax - 1)) != 0) {
186186
/* Ceiling under which ZEND_LONG_MAX % max == 0 */
187187
zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
188188

0 commit comments

Comments
 (0)