/** Writes a right-zero-extended value to {@code out}. */
  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 signed integral to {@code out}. */
  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 an unsigned integral to {@code out}. */
  public static void writeUnsignedIntegralValue(ByteOutput out, int type, long value) {
    // Figure out how many bits are needed to represent the value.
    int requiredBits = 64 - Long.numberOfLeadingZeros(value);
    if (requiredBits == 0) {
      requiredBits = 1;
    }

    // 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--;
    }
  }