我继续我之前的问题。我正在制作 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);