getBit static method

bool getBit(
  1. Uint8List bytes,
  2. int bitIndex
)

Get specific bit from byte array.

Parameters:

  • bytes: Byte array
  • bitIndex: Bit index (0-based)

Returns: true if bit is set, false otherwise

Example:

final coils = await client.readCoils(1, 0, 16);
final isCoil5On = DataConverter.getBit(coils, 5);

Implementation

static bool getBit(Uint8List bytes, int bitIndex) {
  final byteIndex = bitIndex ~/ 8;
  final bitOffset = bitIndex % 8;
  if (byteIndex >= bytes.length) {
    throw RangeError('Bit index $bitIndex out of range');
  }
  return (bytes[byteIndex] & (1 << bitOffset)) != 0;
}