.NET 中的格雷码

发布于 2024-08-09 20:00:45 字数 154 浏览 15 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生殊途 2024-08-16 20:00:45

使用这个技巧

/*
        The purpose of this function is to convert an unsigned
        binary number to reflected binary Gray code.
*/
unsigned short binaryToGray(unsigned short num)
{
        return (num>>1) ^ num;
}

一个棘手的技巧:对于最多 2^n 位,您可以将格雷转换为二进制:
执行 (2^n) - 1 次二进制到格雷的转换。您所需要的只是
上面的函数和一个“for”循环。

/*
        The purpose of this function is to convert a reflected binary
        Gray code number to a binary number.
*/
unsigned short grayToBinary(unsigned short num)
{
        unsigned short temp = num ^ (num>>8);
        temp ^= (temp>>4);
        temp ^= (temp>>2);
        temp ^= (temp>>1);
       return temp;
}

Use this trick.

/*
        The purpose of this function is to convert an unsigned
        binary number to reflected binary Gray code.
*/
unsigned short binaryToGray(unsigned short num)
{
        return (num>>1) ^ num;
}

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.

/*
        The purpose of this function is to convert a reflected binary
        Gray code number to a binary number.
*/
unsigned short grayToBinary(unsigned short num)
{
        unsigned short temp = num ^ (num>>8);
        temp ^= (temp>>4);
        temp ^= (temp>>2);
        temp ^= (temp>>1);
       return temp;
}
浪漫之都 2024-08-16 20:00:45

下面是一个 C# 实现,假设您只想对非负 32 位整数执行此操作:

static uint BinaryToGray(uint num)
{
    return (num>>1) ^ num;
}

您可能还想阅读 这篇博文提供了双向转换的方法,尽管作者选择将代码表示为 int 每个位置包含 1 或 0。就我个人而言,我认为 BitArray 可能是更好的选择。

Here is a C# implementation that assumes you only want to do this on non-negative 32-bit integers:

static uint BinaryToGray(uint num)
{
    return (num>>1) ^ num;
}

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 a BitArray might be a better choice.

离笑几人歌 2024-08-16 20:00:45

也许这个方法集合

  • 对于基于 BitArray
  • 双向
  • int 或只是 n 位的情况

很有用。

public static class GrayCode
{
    public static byte BinaryToByte(BitArray binary)
    {
        if (binary.Length > 8)
            throw new ArgumentException("bitarray too long for byte");

        var array = new byte[1];
        binary.CopyTo(array, 0);
        return array[0];
    }

    public static int BinaryToInt(BitArray binary)
    {
        if (binary.Length > 32)
            throw new ArgumentException("bitarray too long for int");

        var array = new int[1];
        binary.CopyTo(array, 0);
        return array[0];
    }

    public static BitArray BinaryToGray(BitArray binary)
    {
        var len = binary.Length;
        var gray = new BitArray(len);
        gray[len - 1] = binary[len - 1]; // copy high-order bit
        for (var i = len - 2; i >= 0; --i)
        {
            // remaining bits 
            gray[i] = binary[i] ^ binary[i + 1];
        }
        return gray;
    }

    public static BitArray GrayToBinary(BitArray gray)
    {
        var len = gray.Length;
        var binary = new BitArray(len);
        binary[len - 1] = gray[len - 1]; // copy high-order bit
        for (var i = len - 2; i >= 0; --i)
        {
            // remaining bits 
            binary[i] = !gray[i] ^ !binary[i + 1];
        }
        return binary;
    }

    public static BitArray ByteToGray(byte value)
    {
        var bits = new BitArray(new[] { value });
        return BinaryToGray(bits);
    }

    public static BitArray IntToGray(int value)
    {
        var bits = new BitArray(new[] { value });
        return BinaryToGray(bits);
    }

    public static byte GrayToByte(BitArray gray)
    {
        var binary = GrayToBinary(gray);
        return BinaryToByte(binary);
    }

    public static int GrayToInt(BitArray gray)
    {
        var binary = GrayToBinary(gray);
        return BinaryToInt(binary);
    }

    /// <summary>
    ///     Returns the bits as string of '0' and '1' (LSB is right)
    /// </summary>
    /// <param name="bits"></param>
    /// <returns></returns>
    public static string AsString(this BitArray bits)
    {
        var sb = new StringBuilder(bits.Length);
        for (var i = bits.Length - 1; i >= 0; i--)
        {
            sb.Append(bits[i] ? "1" : "0");
        }
        return sb.ToString();
    }

    public static IEnumerable<bool> Bits(this BitArray bits)
    {
        return bits.Cast<bool>();
    }

    public static bool[] AsBoolArr(this BitArray bits, int count)
    {
        return bits.Bits().Take(count).ToArray();
    }
}

Perhaps this collection of methods is useful

  • based on BitArray
  • both directions
  • int or just n Bits

just enjoy.

public static class GrayCode
{
    public static byte BinaryToByte(BitArray binary)
    {
        if (binary.Length > 8)
            throw new ArgumentException("bitarray too long for byte");

        var array = new byte[1];
        binary.CopyTo(array, 0);
        return array[0];
    }

    public static int BinaryToInt(BitArray binary)
    {
        if (binary.Length > 32)
            throw new ArgumentException("bitarray too long for int");

        var array = new int[1];
        binary.CopyTo(array, 0);
        return array[0];
    }

    public static BitArray BinaryToGray(BitArray binary)
    {
        var len = binary.Length;
        var gray = new BitArray(len);
        gray[len - 1] = binary[len - 1]; // copy high-order bit
        for (var i = len - 2; i >= 0; --i)
        {
            // remaining bits 
            gray[i] = binary[i] ^ binary[i + 1];
        }
        return gray;
    }

    public static BitArray GrayToBinary(BitArray gray)
    {
        var len = gray.Length;
        var binary = new BitArray(len);
        binary[len - 1] = gray[len - 1]; // copy high-order bit
        for (var i = len - 2; i >= 0; --i)
        {
            // remaining bits 
            binary[i] = !gray[i] ^ !binary[i + 1];
        }
        return binary;
    }

    public static BitArray ByteToGray(byte value)
    {
        var bits = new BitArray(new[] { value });
        return BinaryToGray(bits);
    }

    public static BitArray IntToGray(int value)
    {
        var bits = new BitArray(new[] { value });
        return BinaryToGray(bits);
    }

    public static byte GrayToByte(BitArray gray)
    {
        var binary = GrayToBinary(gray);
        return BinaryToByte(binary);
    }

    public static int GrayToInt(BitArray gray)
    {
        var binary = GrayToBinary(gray);
        return BinaryToInt(binary);
    }

    /// <summary>
    ///     Returns the bits as string of '0' and '1' (LSB is right)
    /// </summary>
    /// <param name="bits"></param>
    /// <returns></returns>
    public static string AsString(this BitArray bits)
    {
        var sb = new StringBuilder(bits.Length);
        for (var i = bits.Length - 1; i >= 0; i--)
        {
            sb.Append(bits[i] ? "1" : "0");
        }
        return sb.ToString();
    }

    public static IEnumerable<bool> Bits(this BitArray bits)
    {
        return bits.Cast<bool>();
    }

    public static bool[] AsBoolArr(this BitArray bits, int count)
    {
        return bits.Bits().Take(count).ToArray();
    }
}
挽手叙旧 2024-08-16 20:00:45

就 .NET 中的格雷码而言,没有任何内置

There is nothing built-in as far as Gray code in .NET.

天赋异禀 2024-08-16 20:00:45

关于格雷码转换的图形解释 - 这可以有一点帮助

Graphical Explanation about Gray code conversion - this can help a little

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文