encodingbinarygenomegray-code

Encoding a value in gray code with floating point with negatives


My objective here is to be able to convert any number between -4.0 and 4.0 into a 5 bit binary string using gray code. I also need to be able to convert back to decimal.

Thanks for any help you can provide.

If it helps, the bigger picture here is that i'm taking the weights from a neural network and mutating them as a binary string.


Solution

  • If you have only 5 bits available, you can only encode 2^5 = 32 different input values.
    The Gray code is useful, if while the input values change slowly, only a single bit each changes in the coded value.
    So maybe the most straightforward implementation is to map your input range -4.0 to 4.0 to the integer range 0 … 31, and then to represent these integers by a standard Gray code, which can easily be converted back to 0 … 31 and then to -4.0 to 4.0.