public static void writeRightZeroExtendedValue(ByteOutput out, int type, long value) {
    // Figure out how many bits are needed to represent the value.
    int requiredBits = 64 - Long.numberOfTrailingZeros(value);
    if (requiredBits == 0) {
      requiredBits = 1;
    }

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

    // Scootch the first bits to be written down to the low-order bits.
    value >>= 64 - (requiredBytes * 8);

    /*
     * 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 right-zero-extended value to {@code out}.
android/android_framework/dex-maker/src/main/java/com/android/dex/EncodedValueCodec.java