Debian Codegraph @

src/Common/BitUtils.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Common;
5
6 /**
7 * General bit utilities.
8 *
9 * All utility methods are based on 32-bit integers and also work on 64-bit
10 * systems.
11 */
12 final class BitUtils
13 {
14 private function __construct()
15 {
16 }
17
18 /**
19 * Performs an unsigned right shift.
20 *
21 * This is the same as the unsigned right shift operator ">>>" in other
22 * languages.
23 */
24 public static function unsignedRightShift(int $a, int $b) : int
25 {
26 return (
27 $a >= 0
28 ? $a >> $b
29 : (($a & 0x7fffffff) >> $b) | (0x40000000 >> ($b - 1))
30 );
31 }
32
33 /**
34 * Gets the number of trailing zeros.
35 */
36 public static function numberOfTrailingZeros(int $i) : int
37 {
38 $lastPos = strrpos(str_pad(decbin($i), 32, '0', STR_PAD_LEFT), '1');
39 return $lastPos === false ? 32 : 31 - $lastPos;
40 }
41 }