binaryToGray static method

LogicValue binaryToGray(
  1. LogicValue binary
)

Converts a binary value represented by binary to Gray code.

Given a binary value, this function return LogicValue representing the equivalent Gray code.

For each bit in the binary input, starting from the least significant bit (index 0), the function calculates the corresponding Gray code bit based on XOR operation with the previous bit. The resulting Gray code bits are returned.

Returns LogicValue representing the Gray code.

Implementation

static LogicValue binaryToGray(LogicValue binary) {
  final reverseBit = binary.reversed;
  final binList = reverseBit.toList().asMap().entries.map((entry) {
    final currentBit = entry.value;
    final idx = entry.key;
    if (idx == 0) {
      return currentBit;
    } else {
      final previousIndex = reverseBit[idx - 1];
      return currentBit ^ previousIndex;
    }
  }).toList();
  return binList.swizzle();
}