public static void writeSignedIntegralValue(ByteOutput out, int type, long value) {
    /*
     * Figure out how many bits are needed to represent the value,
     * including a sign bit: The bit count is subtracted from 65
     * and not 64 to account for the sign bit. The xor operation
     * has the effect of leaving non-negative values alone and
     * unary complementing negative values (so that a leading zero
     * count always returns a useful number for our present
     * purpose).
     */
    int requiredBits = 65 - Long.numberOfLeadingZeros(value ^ (value >> 63));

    // Round up the requiredBits to a number of bytes.
    int requiredBytes = (requiredBits + 0x07) >> 3;

    /*
     * Write the header byte, which includes the type and
     * requiredBytes - 1.
     */
    out.writeByte(type | ((requiredBytes - 1) << 5));

    // Write the value, per se.
    while (requiredBytes > 0) {
      out.writeByte((byte) value);
      value >>= 8;
      requiredBytes--;
    }
  }
Writes a signed integral to {@code out}.
android/android_framework/dex-maker/src/main/java/com/android/dex/EncodedValueCodec.java