private function getByteNumber($limit)
    {
        $bits = 1;
        $mask = 1;

        while ($limit >> $bits > 0) {
            $mask |= 1 << $bits;
            $bits++;
        }

        $bytes = (int) ceil($bits / 8);

        do {
            $result = $this->readByteNumber($bytes) & $mask;
        } while ($result > $limit);

        return $result;
    }