bitlengthInBytes static method

int bitlengthInBytes(
  1. int value, {
  2. bool sign = false,
})

Minimum number of bytes needed to hold value (add a sign bit when sign is true).

Implementation

static int bitlengthInBytes(int value, {bool sign = false}) {
  if (value < 0 && !sign) {
    throw CoreErrors.invalidOperationArguments(
      'bitlengthInBytes',
      name: 'value',
      reason: 'Negative value requires sign: true.',
    );
  }
  int bits = value.bitLength;
  if (sign) bits += 1;
  if (bits == 0) return 1;
  return (bits + 7) ~/ 8;
}