setBit static method
Set specific bit in byte array.
Parameters:
bytes: Byte arraybitIndex: Bit index (0-based)value: true to set bit, false to clear bit
Example:
final coils = Uint8List(2);
DataConverter.setBit(coils, 5, true);
await client.writeMultipleCoils(1, 0, 16, coils);
Implementation
static void setBit(Uint8List bytes, int bitIndex, bool value) {
final byteIndex = bitIndex ~/ 8;
final bitOffset = bitIndex % 8;
if (byteIndex >= bytes.length) {
throw RangeError('Bit index $bitIndex out of range');
}
if (value) {
bytes[byteIndex] |= (1 << bitOffset);
} else {
bytes[byteIndex] &= ~(1 << bitOffset);
}
}