【问题标题】:C# Parity Bits from a Binary Number [duplicate]来自二进制数的 C# 奇偶校验位 [重复]
【发布时间】:2012-02-27 10:57:25
【问题描述】:

可能重复:
How to add even parity bit on 7-bit binary number

这是我的新代码,它将 7 位二进制数转换为具有偶校验的 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();
    }

}

}

【问题讨论】:

  • 问题是我不知道如何使用奇偶校验和二进制,所以我需要帮助。这是我保存的工作,这并不多,因为我不知道平价。 Console.WriteLine("请输入 7 位二进制数:"); int a = Convert.ToInt32(Console.ReadLine());
  • 你是指哪个奇偶校验?偶数还是奇数?
  • 一个7位数字可以表示为一个7个字符的字符串,每个字符必须是'0''1',也可以表示为整数类型的值范围 0-127。您以“您使用的唯一数字......”开头的句子暗示了前一种方法,但只有在您采用后一种方法时才能调用Convert.ToInt32

标签: c# binary parity


【解决方案1】:

对从 Console.ReadLine() 获得的内容使用 int.TryParse()。然后,您需要检查该数字是否介于 0 和 127 之间,以确保它仅使用 7 位。然后,您需要计算数字的二进制表示中 1 的数量。并将数字加 128 以设置奇偶校验位,具体取决于您指定奇校验还是偶校验。

数 1 是你真正的家庭作业。

【讨论】:

  • 那你将如何在 c# 代码中做到这一点?
  • 只回答值得 +1 的答案,因为所有其他人都对标记为 homework 的内容提供了帮助
  • @AsharAslam 家庭作业的重点是学习,而不是让别人为你做 :) 尽管有些人可能会反对。
【解决方案2】:

通过使用BitArray 类,您可以编写

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

这会将字节的单个位转换为BitArray,它表示可以以简单方式处理的布尔数组。请注意,BitArray 的构造函数接受一个字节数组。由于我们只有一个字节,我们必须向它传递一个长度为 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 是偶数,则x % 2 产生0。如果你想要一个奇校验位,你可以测试count % 2 == 0

BitArray 有一个 CopyTo 方法将我们的位转换回字节数组(在我们的例子中只包含一个字节)。

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

numberAsByte[0] 包含我们的编号和奇偶校验位。


如果你想要右边的奇偶校验位,那么你必须先将数字向左移动一位。

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;

【讨论】:

  • @LB:好的,我以后会考虑到这一点。我以一种更具教学性的方式制定了我的答案。但是,您提供的链接还说:“不要对真诚回答家庭作业问题的其他人投反对票,即使他们违反了这些准则”。
  • @OlivierJacot-Descombes:很抱歉,它说找不到 BitArray。另外,我想向用户显示带有奇偶校验的结束二进制数。
  • BitArray 位于 System.Collections 命名空间中。 Visual Studio 将向您显示一个智能标记,它会为您导入命名空间。将号码呈现给用户应该不是问题。要么按原样打印数字,要么遍历位并打印它们。
  • 我已将 BitArray 放在命名空间区域,但现在它说它是命名空间但用作类型。它有什么问题?
【解决方案3】:

根据wikipedia,奇偶校验位有两种变化,所以我实现了参数来选择你需要的那个。它支持高达 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);
}
猜你喜欢
  • 2013-04-21
  • 2015-04-04
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 2014-03-06
相关资源
最近更新 更多