简体   繁体   English

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

[英]How to add even parity bit on 7-bit binary number

I am continuing from my previous question. 我从上一个问题继续。 I am making ac# 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. 我正在制作一个ac#程序,其中用户输入7位二进制数,然后计算机打印出该数字,并在该数字的右边加上一个偶校验位。 I am struggling. 我在挣扎。 I have a code, but it says BitArray is a namespace but is used as a type. 我有一个代码,但是它说BitArray是一个命名空间,但被用作类型。 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);

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. 然后,如果要偶校验将位7设置为奇数,将奇校验设置为不奇数。

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.... 该电路是双功能电路,返回0和1或1和0,一次也执行1位以上的操作,但这对于TPL来说有点光。

PS you might want to check the input for < 128 otherwise things are going to go well wrong. PS,您可能想检查输入是否小于128,否则会出错。

ooh didn't notice the homework tag, don't use this unless you can explain it. 哦,没有注意到作业标签,除非可以解释,否则不要使用它。

Almost the same process, only much faster on a larger number of bits. 几乎相同的过程,但在大量位数上却要快得多。 Using only the arithmetic operators (SHR && XOR), without loops: 仅使用算术运算符(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);
}

Using a BitArray does not buy you much here, if anything it makes your code harder to understand. 在这里使用BitArray不会给您带来很多好处,如果有的话,它会使您的代码更难以理解。 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: 例如,要确定数字中是否设置了某个位,您可以&将该数字与2的幂次幂相乘。这将导致:

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. 现在剩下的唯一事情就是确定bitsSet是偶数还是奇数,然后在必要时设置剩余的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM