如何在7位二进制数上添加偶校验位
我继续我之前的问题。我正在制作 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);
I am continuing from my previous question. I am making a c# 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. I am struggling. I have a code, but it says BitArray is a namespace but is used as a type. 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
复制他们用来执行此操作的电路可能会更有趣。
然后,如果您想要偶校验,请将第 7 位设置为奇数,将奇校验设置为非奇数。
或者
该电路是双函数返回 0 和 1 或 1 和 0,一次也执行超过 1 位的操作,但这对于 TPL 来说有点轻......
PS 您可能需要检查输入是否 << 128 否则事情就会出问题。
哦,没有注意到作业标签,除非你能解释它,否则不要使用它。
Might be more fun to duplicate the circuit they use to do this..
Then if you want even parity set bit 7 to odd, odd parity to not odd.
or
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....
PS you might want to check the input for < 128 otherwise things are going to go well wrong.
ooh didn't notice the homework tag, don't use this unless you can explain it.
几乎相同的过程,只是在位数更多时速度更快。仅使用算术运算符 (SHR && XOR),不使用循环:
Almost the same process, only much faster on a larger number of bits. Using only the arithmetic operators (SHR && XOR), without loops:
使用
BitArray
在这里并没有什么好处,如果有的话,它会让你的代码更难理解。您的问题可以通过使用&
和|
以及<<
运算符进行基本位操作来解决。例如,要查明某个数字中是否设置了某个位,您可以 &相应的 2 次幂的数字。这导致:
现在唯一剩下的事情就是确定
bitsSet
是偶数还是奇数,然后在必要时设置剩余的位。Using a
BitArray
does not buy you much here, if anything it makes your code harder to understand. 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:
Now the only thing remain is determining if
bitsSet
is even or odd and then setting the remaining bit if necessary.