.NET 中的格雷码
.NET 框架中的任何位置是否都有内置的 格雷码 数据类型?或者格雷码和二进制之间的转换实用程序?我自己可以做,但如果轮子已经发明了……
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用这个技巧。
一个棘手的技巧:对于最多 2^n 位,您可以将格雷转换为二进制:
执行 (2^n) - 1 次二进制到格雷的转换。您所需要的只是
上面的函数和一个“for”循环。
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.
下面是一个 C# 实现,假设您只想对非负 32 位整数执行此操作:
您可能还想阅读 这篇博文提供了双向转换的方法,尽管作者选择将代码表示为
int
每个位置包含 1 或 0。就我个人而言,我认为 BitArray 可能是更好的选择。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.也许这个方法集合
很有用。
Perhaps this collection of methods is useful
just enjoy.
就 .NET 中的格雷码而言,没有任何内置。
There is nothing built-in as far as Gray code in .NET.
关于格雷码转换的图形解释 - 这可以有一点帮助
Graphical Explanation about Gray code conversion - this can help a little