1

重複の可能性:
7ビットの2進数に偶数パリティビットを追加する方法

これは、7ビットの2進数を偶数パリティの8ビットに変換する私の新しいコードです。ただし、動作しません。たとえば、0101010と入力すると、偶数パリティの数は147と表示されます。何が問題なのかを教えてください。

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{


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 };
        System.Collections.BitArray bits = new System.Collections.BitArray(numberAsByte);
        a = a << 1;

        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 number with an even parity bit is:");
        Console.Write(a);
        Console.ReadLine();
    }

}

}

4

3 に答える 3

6

Console.ReadLine() から取得したものに対して int.TryParse() を使用します。次に、数値が 0 から 127 の間であることを確認して、7 ビットのみを使用するようにする必要があります。次に、数値のバイナリ表現で 1 の数をカウントする必要があります。奇数または偶数パリティのどちらを指定したかに応じて、パリティ ビットを設定する数値に 128 を追加します。

1 を数えることは本当の宿題です。

于 2012-02-05T19:36:42.643 に答える
3

BitArrayあなたが書くことができるクラスを使用することによって

int a = Convert.ToInt32(Console.ReadLine());
byte[] numberAsByte = new byte[] { (byte)a };
BitArray bits = new BitArray(numberAsByte);

これにより、バイトの単一ビットがBitArray、簡単な方法で処理できるブール値の配列を表す に変換されます。のコンストラクターはBitArrayバイト配列を受け入れることに注意してください。1 バイトしかないため、この 1 バイトを含む長さ 1 のバイト配列を渡す必要があります ( numberAsByte)。

次に、設定されているビットを数えましょう。

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

でビットをテストするだけでbits[i]、ブール値が得られることに注意してください。完全に合法で正しいというテストbits[i] == trueは同じ結果をもたらしますが、不必要に複雑です。このifステートメントは比較を必要としません。必要なのはブール値だけです。

これにより、奇数パリティ ビットが計算されます。

if (count % 2 == 1) { // Odd number of bits
    bits[7] = true; // Set the left most bit as parity bit for even parity.
}

%演算子はモジュロ演算子です。整数除算の残りを生成します。x % 2が偶数の0場合に返さxれます。奇数パリティ ビットが必要な場合は、count % 2 == 0代わりにテストできます。

BitArrayには、CopyToビットをバイト配列 (この場合は 1 バイトのみを含む) に変換するメソッドがあります。

bits.CopyTo(numberAsByte, 0);
a = numberAsByte[0];

numberAsByte[0]パリティビット付きの番号が含まれています。


右側にパリティ ビットが必要な場合は、最初に数値を 1 ビット左にシフトする必要があります。

int a = Convert.ToInt32(Console.ReadLine());
a = a << 1;

// Do the parity bit calculation as above and, if necessary
// set the right most bit as parity bit.
bits[0] = true;
于 2012-02-05T19:42:34.373 に答える
0

ウィキペディアによると、パリティ ビットには 2 つのバリエーションがあるため、必要なものを選択するパラメーターを実装しました。63 ビットまでのユーザー入力をサポートします。検証コードの実装はあなたに任せます。

ulong GetNumberParity(string input, bool isEvenParity)
{
    ulong tmp = Convert.ToUInt64(input, 2);
    ulong c = 0;
    for (int i = 0; i < 64; i++) c += tmp >> i & 1;
    if(isEvenParity)
        return Convert.ToUInt64((c % 2 != 0 ? "1" : "0") + input, 2);
    else
        return Convert.ToUInt64((c % 2 == 0? "1" : "0") + input, 2);
}
于 2012-02-05T19:34:13.250 に答える