如何在7位二进制数上添加偶校验位

发布于 2025-01-03 04:33:43 字数 966 浏览 2 评论 0原文

我继续我之前的问题。我正在制作 ac# 程序,用户输入一个 7 位二进制数,计算机打印出该数字,该数字右侧带有偶校验位。我正在挣扎。我有一个代码,但它说 BitArray 是一个命名空间,但用作类型。另外,有没有办法改进代码并使其更简单?

namespace BitArray
{
    class Program
    {    
        static void Main(string[] args)    
        {
            Console.WriteLine("Please enter a 7-bit binary number:");
            int a = Convert.ToInt32(Console.ReadLine());
            byte[] numberAsByte = new byte[] { (byte)a };
            BitArray bits = new BitArray(numberAsByte);
            int count = 0;

            for (int i = 0; i < 8; i++)
            {
                if (bits[i])
                {
                    count++;
                }
            }

            if (count % 2 == 1)
            {
                bits[7] = true;
            }

            bits.CopyTo(numberAsByte, 0);
            a = numberAsByte[0];
            Console.WriteLine("The binary number with a parity bit is:");
            Console.WriteLine(a);

I am continuing from my previous question. I am making a c# program where the user enters a 7-bit binary number and the computer prints out the number with an even parity bit to the right of the number. I am struggling. I have a code, but it says BitArray is a namespace but is used as a type. Also, is there a way I could improve the code and make it simpler?

namespace BitArray
{
    class Program
    {    
        static void Main(string[] args)    
        {
            Console.WriteLine("Please enter a 7-bit binary number:");
            int a = Convert.ToInt32(Console.ReadLine());
            byte[] numberAsByte = new byte[] { (byte)a };
            BitArray bits = new BitArray(numberAsByte);
            int count = 0;

            for (int i = 0; i < 8; i++)
            {
                if (bits[i])
                {
                    count++;
                }
            }

            if (count % 2 == 1)
            {
                bits[7] = true;
            }

            bits.CopyTo(numberAsByte, 0);
            a = numberAsByte[0];
            Console.WriteLine("The binary number with a parity bit is:");
            Console.WriteLine(a);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

贵在坚持 2025-01-10 04:33:43

复制他们用来执行此操作的电路可能会更有趣。

bool odd = false;

for(int i=6;i>=0;i--)
  odd ^= (number & (1 << i)) > 0;

然后,如果您想要偶校验,请将第 7 位设置为奇数,将奇校验设置为非奇数。

或者

bool even = true;

for(int i=6;i>=0;i--)
  even ^= (number & (1 << i)) > 0;

该电路是双函数返回 0 和 1 或 1 和 0,一次也执行超过 1 位的操作,但这对于 TPL 来说有点轻......

PS 您可能需要检查输入是否 << 128 否则事情就会出问题。

哦,没有注意到作业标签,除非你能解释它,否则不要使用它。

Might be more fun to duplicate the circuit they use to do this..

bool odd = false;

for(int i=6;i>=0;i--)
  odd ^= (number & (1 << i)) > 0;

Then if you want even parity set bit 7 to odd, odd parity to not odd.

or

bool even = true;

for(int i=6;i>=0;i--)
  even ^= (number & (1 << i)) > 0;

The circuit is dual function returns 0 and 1 or 1 and 0, does more than 1 bit at a time as well, but this is a bit light for TPL....

PS you might want to check the input for < 128 otherwise things are going to go well wrong.

ooh didn't notice the homework tag, don't use this unless you can explain it.

双马尾 2025-01-10 04:33:43

几乎相同的过程,只是在位数更多时速度更快。仅使用算术运算符 (SHR && XOR),不使用循环:

public static bool is_parity(int data)
{
    //data ^= data >> 32; // if arg >= 64-bit (notice argument length)
    //data ^= data >> 16; // if arg >= 32-bit 
    //data ^= data >> 8;  // if arg >= 16-bit
    data ^= data >> 4;
    data ^= data >> 2;
    data ^= data >> 1;
    return (data & 1) !=0;
}

public static byte fix_parity(byte data)
{
    if (is_parity(data)) return data;
    return (byte)(data ^ 128);
}

Almost the same process, only much faster on a larger number of bits. Using only the arithmetic operators (SHR && XOR), without loops:

public static bool is_parity(int data)
{
    //data ^= data >> 32; // if arg >= 64-bit (notice argument length)
    //data ^= data >> 16; // if arg >= 32-bit 
    //data ^= data >> 8;  // if arg >= 16-bit
    data ^= data >> 4;
    data ^= data >> 2;
    data ^= data >> 1;
    return (data & 1) !=0;
}

public static byte fix_parity(byte data)
{
    if (is_parity(data)) return data;
    return (byte)(data ^ 128);
}
计㈡愣 2025-01-10 04:33:43

使用 BitArray 在这里并没有什么好处,如果有的话,它会让你的代码更难理解。您的问题可以通过使用 &| 以及 << 运算符进行基本位操作来解决。

例如,要查明某个数字中是否设置了某个位,您可以 &相应的 2 次幂的数字。这导致:

int bitsSet = 0;
for(int i=0;i<7;i++)
    if ((number & (1 << i)) > 0)
        bitsSet++;

现在唯一剩下的事情就是确定 bitsSet 是偶数还是奇数,然后在必要时设置剩余的位。

Using a BitArray does not buy you much here, if anything it makes your code harder to understand. Your problem can be solved with basic bit manipulation with the & and | and << operators.

For example to find out if a certain bit is set in a number you can & the number with the corresponding power of 2. That leads to:

int bitsSet = 0;
for(int i=0;i<7;i++)
    if ((number & (1 << i)) > 0)
        bitsSet++;

Now the only thing remain is determining if bitsSet is even or odd and then setting the remaining bit if necessary.

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