-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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1726553
  • 13
  • 2
  • 7

3 Answers3

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);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Consider the following user input: "123456". The logic does not validate user input. – Sergey Vyacheslavovich Brunov Feb 11 '13 at 18:19
  • @SergeyBrunov I said that in the first paragraph. – poke Feb 11 '13 at 19:21
  • @poke, so I think there is no need to develop the idea because **it is not safe**. Why not convert the input to integer representation, process it and print output? – Sergey Vyacheslavovich Brunov Feb 12 '13 at 06:04
  • @SergeyBrunov I consider StackOverflow to be about *learning* in the first place; not something you can throw some random code into and get perfect and safe code back. I think it is important to first pursue OP’s own idea so he understands what he did wrong there, before providing a completely different—and likely more complex—one. Also I don’t consider the parse-to-int way better as you may not always have the full bit representation available (it’s like that on the hardware). Simply checking for invalid characters as you go is way easier (there are only two valid characters after all). – poke Feb 12 '13 at 10:04
  • @poke, completely agree with you about the concept of StackOverflow. – Sergey Vyacheslavovich Brunov Feb 12 '13 at 10:19
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);