Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented...
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Use this trick.
A tricky Trick: for up to 2^n bits, you can convert Gray to binary by performing (2^n) - 1 binary-to Gray conversions. All you need is the function above and a 'for' loop.
Perhaps this collection of methods is useful
just enjoy.
There is nothing built-in as far as Gray code in .NET.
Graphical Explanation about Gray code conversion - this can help a little
Here is a C# implementation that assumes you only want to do this on non-negative 32-bit integers:
You might also like to read this blog post which provides methods for conversions in both directions, though the author chose to represent the code as an array of
int
containing either one or zero at each position. Personally I would think aBitArray
might be a better choice.