public function testNumberOfTrailingZeros() { $this->assertEquals(32, BitUtils::numberOfTrailingZeros(0)); $this->assertEquals(1, BitUtils::numberOfTrailingZeros(10)); $this->assertEquals(0, BitUtils::numberOfTrailingZeros(15)); $this->assertEquals(2, BitUtils::numberOfTrailingZeros(20)); }
/** * Returns the mask bit for "getMaskPattern" at "x" and "y". * * See 8.8 of JISX0510:2004 for mask pattern conditions. * * @param integer $maskPattern * @param integer $x * @param integer $y * @return integer * @throws Exception\InvalidArgumentException */ public static function getDataMaskBit($maskPattern, $x, $y) { switch ($maskPattern) { case 0: $intermediate = $y + $x & 0x1; break; case 1: $intermediate = $y & 0x1; break; case 2: $intermediate = $x % 3; break; case 3: $intermediate = ($y + $x) % 3; break; case 4: $intermediate = BitUtils::unsignedRightShift($y, 1) + $x / 3 & 0x1; break; case 5: $temp = $y * $x; $intermediate = ($temp & 0x1) + $temp % 3; break; case 6: $temp = $y * $x; $intermediate = ($temp & 0x1) + $temp % 3 & 0x1; break; case 7: $temp = $y * $x; $intermediate = $temp % 3 + ($y + $x & 0x1) & 0x1; break; default: throw new Exception\InvalidArgumentException('Invalid mask pattern: ' . $maskPattern); } return $intermediate === 0; }
/** * Gets the most bottom right set bit. * * This is useful in detecting a corner of a 'pure' barcode. * * @return SplFixedArray */ public function getBottomRightOnBit() { $bitsOffset = count($this->bits) - 1; while ($bitsOffset >= 0 && $this->bits[$bitsOffset] === 0) { $bitsOffset--; } if ($bitsOffset < 0) { return null; } $x = intval($bitsOffset / $this->rowSize); $y = $bitsOffset % $this->rowSize << 5; $bits = $this->bits[$bitsOffset]; $bit = 0; while (BitUtils::unsignedRightShift($bits, $bit) === 0) { $bit--; } $x += $bit; return SplFixedArray::fromArray(array($x, $y), false); }
/** * Gets the next unset bit position from a given position. * * @param integer $from * @return integer */ public function getNextUnset($from) { if ($from >= $this->size) { return $this->size; } $bitsOffset = $from >> 5; $currentBits = ~$this->bits[$bitsOffset]; $bitsLength = count($this->bits); $currentBits &= ~((1 << ($from & 0x1f)) - 1); while ($currentBits === 0) { if (++$bitsOffset === $bitsLength) { return $this->size; } $currentBits = ~$this->bits[$bitsOffset]; } $result = ($bitsOffset << 5) + BitUtils::numberOfTrailingZeros($currentBits); return $result > $this->size ? $this->size : $result; }