grayToBinary static method

LogicValue grayToBinary(
  1. LogicValue gray
)

Converts a Gray code value represented by gray to binary.

Given a gray value, this function return LogicValue representing the equivalent binary representation.

For each bit in the gray input, starting from the least significant bit (index 0), the function calculates the corresponding binary bit based on XOR operation with the previous binary bit.

Return LogicValue representing the binary representation.

Implementation

static LogicValue grayToBinary(LogicValue gray) {
  final reverseGray = gray.reversed;
  final grayList = reverseGray.toList();
  var previousBit = LogicValue.zero;

  final binaryList = grayList.map((currentBit) {
    final binaryBit = currentBit ^ previousBit;
    previousBit = binaryBit;
    return binaryBit;
  }).toList();

  return binaryList.swizzle();
}