1

Possible Duplicate:
How to add even parity bit on 7-bit binary number

This is my new code which converts a 7-bit binary number to an 8-bit with even parity. However it does not work. When I type in 0101010 for example, it says the number with even parity is 147. Could you help, showing me what is wrong please?

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();
    }

}

}

Community
  • 1
  • 1
  • The thing is that I'm not sure how to use parity and binary, so I need help. This is my saved work, which is not much as I don't know about parity. Console.WriteLine("Please enter a 7-bit binary number:"); int a = Convert.ToInt32(Console.ReadLine()); –  Feb 05 '12 at 19:19
  • Which parity do you mean? Even or odd? – Sergey Vyacheslavovich Brunov Feb 05 '12 at 19:26
  • 1
    A 7-bit number could be expressed as a string of seven characters, each of which must be either `'0'` or `'1'`, or it could be expressed as a value of an integral type in the range 0-127. Your sentence beginning with "the only numbers you use..." implies the former approach, but the call to `Convert.ToInt32` would only work if you take the latter approach. – phoog Feb 05 '12 at 19:28

3 Answers3

5

Use int.TryParse() on what you got from Console.ReadLine(). You then need to check that the number is between 0 and 127 to ensure it uses only 7 bits. You then need to count the number of 1s in the binary representation of the number. And add 128 to the number to set the parity bit, depending whether you specified odd or even parity.

Counting 1s is your real homework assignment.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

By using the BitArray class you can write

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

This converts the single bits of your byte to a BitArray, which represents an array of Booleans that can be handled in a easy way. Note that the constructor of BitArray accepts an array of bytes. Since we have only one byte, we have to pass it a byte array of length 1 containing this single byte (numberAsByte).

Now let us count the bits that are set.

int count = 0;
for (int i = 0; i < 8; i++) {
    if (bits[i]) {
        count++;
    }
}

Note that we simply test for a bit with bits[i], which yields a Boolean value. The test bits[i] == true as being perfectly legal and correct yields the same result but is unnecessarily complicated. The if statement does not require a comparison. All it wants is a Boolean value.

This calculates an odd parity bit.

if (count % 2 == 1) { // Odd number of bits
    bits[7] = true; // Set the left most bit as parity bit for even parity.
}

The % operator is the modulo operator. It yields the rest of an integer division. x % 2 yields 0 if x is even. If you want an odd parity bit, you can test count % 2 == 0 instead.

BitArray has a CopyTo method which converts our bits back to an array of bytes (containing only one byte in our case).

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

numberAsByte[0] contains our number with a parity bit.


If you want the parity bit on the right side, then you will have to shift the number to the left by one bit first.

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;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions – L.B Feb 05 '12 at 22:07
  • @LB: OK, I will take that in account in the future. I have formulated my answer in a more didactical way. However, the link you provided also says, "Don't downvote others who answer homework questions in good faith, even if they break these guidelines". – Olivier Jacot-Descombes Feb 06 '12 at 15:32
  • @OlivierJacot-Descombes: I'm sorry to ask but it says that BitArray cannot be found. Also, I want to show the end binary number with parity as a result to the user. –  Feb 06 '12 at 18:12
  • `BitArray` is in the `System.Collections` namespace. Visual Studio will show you a smart tag, which imports the namespace for you. Presenting the number to the user should not be a problem. Either print the number as is or loop through the bits and print them. – Olivier Jacot-Descombes Feb 06 '12 at 18:31
  • I have put BitArray in the namespace area but now it has said it is a namespace but is used as a type. What is the problem with it? –  Feb 06 '12 at 18:45
  • You need a `using System.Collections;` in the header. – Olivier Jacot-Descombes Feb 07 '12 at 13:56
  • I have edited the code. I put it in but BitArray hasn't appeared in green. What is wrong? Will the rest be fine? –  Feb 07 '12 at 20:20
  • You have two possibilities. 1: Import the namespace with `using System.Collections;` and then create the BitArray with `BitArray bits = new BitArray(numberAsByte);`. 2: Don't import the namespace and create the BitArray with `System.Collections.BitArray bits = new System.Collections.BitArray(numberAsByte);`. If the compiler does not complain, you will be fine. Otherwise it will tell you what is wrong. – Olivier Jacot-Descombes Feb 07 '12 at 20:30
  • Now it says that the binary number 0101010 with an even parity bit is 147. Why is this happening? I'll update my code. –  Feb 08 '12 at 17:30
  • I thought the bits were indexed from left to right, but they are indexed from right to left. If you want to have an even number of bits with the parity bit on the left side, change the code to `if (count % 2 == 1) { bits[7] = true; }`. – Olivier Jacot-Descombes Feb 08 '12 at 18:14
  • Now it says that the number with parity is 146. Why? –  Feb 08 '12 at 18:25
  • I get 170 for the number 42. I suggest that you use the debugger. Also insert the code `string s = ""; for (int i = 0; i < 8; i++) { if (bits[i]) s += "1"; else s += "0"; }` in order to see what the `BitArray` contains. – Olivier Jacot-Descombes Feb 09 '12 at 13:39
0

According to wikipedia there are two variation of parity bits, so I implemented parameter to select the one you need. It supports user input up to 63 bits, i'm leaving implementation of validation code to you.

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);
}
Tomek
  • 3,267
  • 2
  • 22
  • 23
  • http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions – L.B Feb 05 '12 at 22:07