前回の質問の続きです。ユーザーが 7 ビットの 2 進数を入力すると、コンピューターが数値の右側に偶数パリティ ビットを付けて数値を出力する ac# プログラムを作成しています。私は苦労しています。コードがありますが、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);