Takes a two's-complement represented value and
    converts it to a signed integer based on the provided bitwidth.
    For use with Simulation.inspect() etc. when expecting negative numbers,
    which it does not recognize
161916
def rev_twos_comp_repr(val, bitwidth):
    
    valbl = val.bit_length()
    if bitwidth < val.bit_length() or val == 2**(bitwidth-1):
        raise pyrtl.PyrtlError("please choose a larger target bitwidth")
    if bitwidth == valbl:  # MSB is a 1, value is negative
        return -((~val & (2**bitwidth-1)) + 1)  # flip the bits, add one, and make negative
    else:
        return val
799428
function( num, signed ) {
		var n = new Buffer(4);
		if (SAFE) { if (signed) {
			if (num < INT32_MIN) 
				throw new Errors.PackError('Packing integer bigger than 32-bits ('+num+')!');
			else if (num > INT32_MAX) 
				throw new Errors.PackError('Packing integer bigger than 32-bits ('+num+')!');
		} else {
			if (num < 0) 
				throw new Errors.PackError('Packing negative integer on unsigned 32-bit ('+num+')!');
			else if (num > UINT32_MAX) 
				throw new Errors.PackError('Packing integer bigger than 32-bits ('+num+')!');
		} }
		if (signed) packViewI32[0] = num;
		else packViewU32[0] = num;
		for (var i=0; i<4; ++i) n[i]=packViewU8[i];
		return n;
	}
383619
static int getUnsignedInt(ByteBuf buf, int offset) {
        return (buf.getByte(offset)     & 0x7F) << 24 |
               (buf.getByte(offset + 1) & 0xFF) << 16 |
               (buf.getByte(offset + 2) & 0xFF) <<  8 |
                buf.getByte(offset + 3) & 0xFF;
    }
1203284
def bitlist_to_int(bitlist):
    

    ret = 0
    for b in bitlist:
        ret = (ret << 1) | (int(b) & 1)
    return ret
148338
def intToBin(i):
    
    # devide in two parts (bytes)
    i1 = i % 256
    i2 = int(i / 256)
    # make string (little endian)
    return chr(i1) + chr(i2)
466275
static BinaryToASCIIConverter getBinaryToASCIIConverter(double d, boolean isCompatibleFormat) {
        long dBits = Double.doubleToRawLongBits(d);
        boolean isNegative = (dBits&DoubleConsts.SIGN_BIT_MASK) != 0; // discover sign
        long fractBits = dBits & DoubleConsts.SIGNIF_BIT_MASK;
        int  binExp = (int)( (dBits&DoubleConsts.EXP_BIT_MASK) >> EXP_SHIFT );
        // Discover obvious special cases of NaN and Infinity.
        if ( binExp == (int)(DoubleConsts.EXP_BIT_MASK>>EXP_SHIFT) ) {
            if ( fractBits == 0L ){
                return isNegative ? B2AC_NEGATIVE_INFINITY : B2AC_POSITIVE_INFINITY;
            } else {
                return B2AC_NOT_A_NUMBER;
            }
        }
        // Finish unpacking
        // Normalize denormalized numbers.
        // Insert assumed high-order bit for normalized numbers.
        // Subtract exponent bias.
        int  nSignificantBits;
        if ( binExp == 0 ){
            if ( fractBits == 0L ){
                // not a denorm, just a 0!
                return isNegative ? B2AC_NEGATIVE_ZERO : B2AC_POSITIVE_ZERO;
            }
            int leadingZeros = Long.numberOfLeadingZeros(fractBits);
            int shift = leadingZeros-(63-EXP_SHIFT);
            fractBits <<= shift;
            binExp = 1 - shift;
            nSignificantBits =  64-leadingZeros; // recall binExp is  - shift count.
        } else {
            fractBits |= FRACT_HOB;
            nSignificantBits = EXP_SHIFT+1;
        }
        binExp -= DoubleConsts.EXP_BIAS;
        BinaryToASCIIBuffer buf = getBinaryToASCIIBuffer();
        buf.setSign(isNegative);
        // call the routine that actually does all the hard work.
        buf.dtoa(binExp, fractBits, nSignificantBits, isCompatibleFormat);
        return buf;
    }
1292187
def _parse_decimal(buf):
    """"""
    exponent = _parse_var_int(buf, signed=True)
    sign_bit, coefficient = _parse_signed_int_components(buf)

    if coefficient == 0:
        # Handle the zero cases--especially negative zero
        value = Decimal((sign_bit, (0,), exponent))
    else:
        coefficient *= sign_bit and -1 or 1
        value = Decimal(coefficient).scaleb(exponent)

    return value
1261993
public static int toInt(byte[] b, int off, boolean littleEndian) {
		if (littleEndian) {
			return (b[off] & 0xFF) | ((b[off + 1] & 0xFF) << 8) |
					((b[off + 2] & 0xFF) << 16) | ((b[off + 3] & 0xFF) << 24);
		}
		return ((b[off] & 0xFF) << 24) | ((b[off + 1] & 0xFF) << 16) |
				((b[off + 2] & 0xFF) << 8) | (b[off + 3] & 0xFF);
	}
676016
public static int flip(int i) {
		int result = 0;
		result |= (i & 0xFF) << 24;
		result |= (i & 0xFF00) << 8;
		result |= ((i & 0xFF0000) >> 8) & 0xFF00;
		result |= ((i & 0xFF000000) >> 24) & 0xFF;
		return result;
	}
333922
def _SignedVarintEncoder():
  """"""

  local_chr = chr
  def EncodeSignedVarint(write, value):
    if value < 0:
      value += (1 << 64)
    bits = value & 0x7f
    value >>= 7
    while value:
      write(0x80|bits)
      bits = value & 0x7f
      value >>= 7
    return write(bits)

  return EncodeSignedVarint
92904
@CheckReturnValue
  public UnsignedInteger minus(UnsignedInteger val) {
    return fromIntBits(value - checkNotNull(val).value);
  }
446099
def _wrapped_cardinality(x, y, bits):
        

        if x == ((y + 1) % (2 ** bits)):
            return 2 ** bits

        else:
            return ((y - x) + 1) & (2 ** bits - 1)
844602
public int readUnsignedShortAsInt(int off) {
    	this.buffer.position(off);
        int b1 = (0x000000FF & (this.buffer.get()));
        int b2 = (0x000000FF & (this.buffer.get()));
        int val = b1 << 8 | b2;
        return val;
    }
1119870
def _setint(self, int_, length=None):
        """"""
        # If no length given, and we've previously been given a length, use it.
        if length is None and hasattr(self, 'len') and self.len != 0:
            length = self.len
        if length is None or length == 0:
            raise CreationError("A non-zero length must be specified with an int initialiser.")
        if int_ >= (1 << (length - 1)) or int_ < -(1 << (length - 1)):
            raise CreationError("{0} is too large a signed integer for a bitstring of length {1}. "
                                "The allowed range is [{2}, {3}].", int_, length, -(1 << (length - 1)),
                                (1 << (length - 1)) - 1)
        if int_ >= 0:
            self._setuint(int_, length)
            return
        # TODO: We should decide whether to just use the _setuint, or to do the bit flipping,
        # based upon which will be quicker. If the -ive number is less than half the maximum
        # possible then it's probably quicker to do the bit flipping...

        # Do the 2's complement thing. Add one, set to minus number, then flip bits.
        int_ += 1
        self._setuint(-int_, length)
        self._invert_all()
752646
def normalize_int(value: IntConvertible) -> int:
    
    if is_integer(value):
        return cast(int, value)
    elif is_bytes(value):
        return big_endian_to_int(value)
    elif is_hex(value) and is_0x_prefixed(value):
        value = cast(str, value)
        if len(value) == 2:
            return 0
        else:
            return int(value, 16)
    elif is_string(value):
        return int(value)
    else:
        raise TypeError("Unsupported type: Got `{0}`".format(type(value)))
377979
def _SignedVarintSize(value):
  """"""
  if value < 0: return 10
  if value <= 0x7f: return 1
  if value <= 0x3fff: return 2
  if value <= 0x1fffff: return 3
  if value <= 0xfffffff: return 4
  if value <= 0x7ffffffff: return 5
  if value <= 0x3ffffffffff: return 6
  if value <= 0x1ffffffffffff: return 7
  if value <= 0xffffffffffffff: return 8
  if value <= 0x7fffffffffffffff: return 9
  return 10
1339958
def decint(n, signed=False):  # pylint: disable=invalid-name,too-many-branches
    

    if isinstance(n, str):
        n = utils.to_string(n)

    if n is True:
        return 1

    if n is False:
        return 0

    if n is None:
        return 0

    if is_numeric(n):
        if signed:
            if not -TT255 <= n <= TT255 - 1:
                raise EncodingError('Number out of range: %r' % n)
        else:
            if not 0 <= n <= TT256 - 1:
                raise EncodingError('Number out of range: %r' % n)

        return n

    if is_string(n):
        if len(n) > 32:
            raise EncodingError('String too long: %r' % n)

        if len(n) == 40:
            int_bigendian = decode_hex(n)
        else:
            int_bigendian = n  # pylint: disable=redefined-variable-type

        result = big_endian_to_int(int_bigendian)
        if signed:
            if result >= TT255:
                result -= TT256

            if not -TT255 <= result <= TT255 - 1:
                raise EncodingError('Number out of range: %r' % n)
        else:
            if not 0 <= result <= TT256 - 1:
                raise EncodingError('Number out of range: %r' % n)

        return result

    raise EncodingError('Cannot decode integer: %r' % n)