@Override
	public byte[] encodeValue(Object value, MemBuffer buf, Settings settings, int length)
			throws DataTypeEncodeException {
		if (length == -1) {
			length = getLength();
		}
		if (length != getLength()) {
			throw new DataTypeEncodeException("Length mismatch", value, this);
		}
		BigInteger bigValue = castValueToEncode(value);
		if (bigValue.signum() == -1 && !isSigned()) {
			throw new DataTypeEncodeException("Unsigned type cannot have negative value", value,
				this);
		}
		BigInteger maxValueExclusive = BigInteger.ONE.shiftLeft(length * 8 - (isSigned() ? 1 : 0));
		BigInteger minValueInclusive = isSigned()
				? BigInteger.ONE.shiftLeft(length * 8 - 1).negate()
				: BigInteger.ZERO;
		if (bigValue.compareTo(maxValueExclusive) >= 0) {
			throw new DataTypeEncodeException("Value is too large", bigValue, this);
		}
		if (minValueInclusive.compareTo(bigValue) > 0) {
			throw new DataTypeEncodeException("Value is too small", bigValue, this);
		}
		return Utils.bigIntegerToBytes(bigValue, length, ENDIAN.isBigEndian(settings, buf));
	}
Get the number of bits in the integral type

@param type the type
@return the number of bits
Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java