-8

Please help I want c# to come up with this once it comes up

Please enter a 7-bit binary number:

0010011

Your number with a parity bit is:

00100111 

This is my code that I have come up with, its probably wrong please can you correct it so it is alright?

{
   Console.WriteLine("Please enter a 7- bit binary number");

   string number = Console.ReadLine();

   int count1 = 0;
   int count2 = 0;

   for(int i = 0; i < number.Length; i++)
   {
      count1++;
   }
   else 
   {
       count2++;
   }

   if (count1 < count2) 
      Console.WriteLine("Your number with parity bit is "+ number+ "1");
}
else
{ 
    Console.WriteLine("Your number with parity bit is "+ number + "0");
}

    }
}

}

Thx in advance for the help

4

3 に答える 3

1

This should do it:

Console.WriteLine("Please enter a 7- bit binary number");

string number = Console.ReadLine();

int count = 0;

for(int i = 0; i < number.Length; i++)
{
   if(number[i] == '1')
   {
      count++;
   }
}

Console.WriteLine("Your number with parity bit is "+ number + (count % 2).ToString());

I'd imagine this is beyond your current level, but this should also work:

Console.WriteLine("Please enter a 7- bit binary number");

string number = Console.ReadLine();

string parityBit = (number.Where(c => c == '1').Count() % 2).ToString();

Console.WriteLine("Your number with parity bit is "+ number + parityBit);
于 2013-02-11T17:31:03.550 に答える
1

So, the parity bit indicates whether or not the input has an even number of one-bits. So if your input string is in binary (which you normally shouldn’t safely assume, but we’ll ignore that for now), you need to simply count the number of 1s in the binary string.

Following your initial idea (I think?), you could do it like this:

int numberOfOnes = 0;
for (int i = 0; i < bitstring.length; i++)
{
    if (bitstring[i] == '1')
        numberOfOnes++;
}

if (numbersOfOnes % 2 == 0)
    // even number of ones
else
    // uneven number of ones

Note that there are two different versions of the parity bit, depending on if you have a 1 if the parity is even, or a 0 if the parity is even. Which you choose is up to you.

于 2013-02-11T17:35:26.170 に答える
1

The algorithm:

  1. Convert the string to integer.
  2. Find the parity bit value.
  3. "Append" parity bit value to the source integer => the result integer.
  4. Convert the result integer to string and write the string to output.

Source code:

Console.Write("Please enter a 7-bit binary number: ");
string numberString = Console.ReadLine();

// Convert to integer representation.
int number = Convert.ToInt32(numberString, 2);

// Find parity bit value for the integer.
bool parity = false;
int tempNumber = number;
while (tempNumber > 0)
{
    parity ^= tempNumber % 2 != 0;
    tempNumber >>= 1;
}

// "Append" parity bit.
int numberWithParity = number << 1 | (parity ? 1 : 0);

// Convert the result to string representation.
numberString = Convert.ToString(numberWithParity, 2).PadLeft(8, '0');
Console.WriteLine("Your number with parity bit is {0}", numberString);
于 2013-02-11T17:51:01.460 に答える