1

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);
Jacob
  • 77,566
  • 24
  • 149
  • 228

3 Answers3

2

Might be more fun to duplicate the circuit they use to do this..

bool odd = false;

for(int i=6;i>=0;i--)
  odd ^= (number & (1 << i)) > 0;

Then if you want even parity set bit 7 to odd, odd parity to not odd.

or

bool even = true;

for(int i=6;i>=0;i--)
  even ^= (number & (1 << i)) > 0;

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.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • thanks, I understand the bool because boolean (true/false). I get the rest, but the only problem is that apparently bits does not exist in the current context. Is it string, bool, int etc.? –  Feb 06 '12 at 20:21
  • The only reference to bits is in your code, neither I nor broken Broken Glass mentioned them. We used shift operator and an index. The class BitArray, which you don't need, is in the System.Collections namespace, but when you put your code in a namespace called BitArray, you hid it. @Hans told you that. Change namespace BitArray in your code to namespace anything else that isn't already used in one the namespaces I'm using. Tip pick better namespaces, Ashar.Homework.Exercise1 would solve your problem. – Tony Hopkinson Feb 06 '12 at 23:13
1

Almost the same process, only much faster on a larger number of bits. Using only the arithmetic operators (SHR && XOR), without loops:

public static bool is_parity(int data)
{
    //data ^= data >> 32; // if arg >= 64-bit (notice argument length)
    //data ^= data >> 16; // if arg >= 32-bit 
    //data ^= data >> 8;  // if arg >= 16-bit
    data ^= data >> 4;
    data ^= data >> 2;
    data ^= data >> 1;
    return (data & 1) !=0;
}

public static byte fix_parity(byte data)
{
    if (is_parity(data)) return data;
    return (byte)(data ^ 128);
}
vGimly
  • 61
  • 1
  • 4
0

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:

int bitsSet = 0;
for(int i=0;i<7;i++)
    if ((number & (1 << i)) > 0)
        bitsSet++;

Now the only thing remain is determining if bitsSet is even or odd and then setting the remaining bit if necessary.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • What should I do with BitArray bits = new BitArray(numberasByte) as it says it is a namespace but used as a type and what should I do with ((number & (1 << i)) > 0) as it says doesn't exist in the current context? Could you please show me in code? –  Feb 06 '12 at 19:58
  • @AsharAslam: you have to define number as int, just as in your example you can use `int number = Convert.ToInt32(Console.ReadLine());`. You don't need to BitArray code at all – BrokenGlass Feb 06 '12 at 20:00
  • now it says bits isn't defined in the current context @Broken Glass. I voted this answer –  Feb 06 '12 at 20:03
  • @AsharAslam: Since this is homework I won't write the code for you, you have to do the transfer work – BrokenGlass Feb 06 '12 at 20:08
  • i'm just wondering, would you, like int number, put something like string, bool, int etc. with bits? –  Feb 06 '12 at 20:13
  • @AsharAslam - Your comment does not make sense. Update your answer and explain what you want exactly. – Security Hound Feb 07 '12 at 20:14