Why is my new BitArray initializing with the wrong value?

Background

I have a console application here where the following things happen...

  1. The user/operator enters a string of bits into the console i.e. "0101". - Works
  2. The string of bits gets converted into a byte. - Works
  3. The byte array gets reversed using my reverse method. - Works
  4. I print out the reversed String in the console window. - Doesn't Work

My issue is with number 4 here. I create a bit array out of the newly reversed reversedBits value, and this is where things seem to go wrong. It's as if reversedBitArray is being initialized with the old/un-reversed bits value instead of reversedBits, as my output always seems to be the un-reversed String. This bug seems like it should be so trivial, the reversedBits is the correctly reversed value, so there is some issue with declaring the reversedBitArray but I can't figure out what. Below is all of my source code, including:

The main console application.

static void Main(string[] args)
{
    BitArray reversedBitArray = null;
    String bitString = String.Empty;
    byte reversedBits = 0;
    byte bits = 0;

    // prompt the user to enter a string of bits
    Console.WriteLine("Enter a string of bits here: ");

    // wait for the user to enter the string of bits
    bitString = Console.ReadLine();

    // convert the string of bits into an integer
    bits = Convert.ToByte(bitString, 2);

    // reverse all of the bits
    reversedBits = ReverseBits(bits);

    // display the reversed bits
    Console.WriteLine("\nThe reversed bit string is...");
    reversedBitArray = new BitArray(new byte[] { reversedBits });
    foreach (bool value in reversedBitArray)
    {
        Console.Write(Convert.ToInt32(value));
    }

    // wait for the user to exit
    Console.WriteLine("\n\nPress any key to exit...");
    while (!Console.KeyAvailable)
    {
        Thread.Sleep(100);
    }

    // exit the console application
    Environment.Exit(0);
}

The bit reversal method.

static byte ReverseBits(byte x)
{
    BitArray myBitArray
    byte[] singleElementArray = null;
    int bitArrayMidPoint = 0;
    int bitArrayLength = 0;

    // get a bit array
    myBitArray = new BitArray(new byte[] { x });

    // get the length and mid-point
    bitArrayLength = myBitArray.Length;
    bitArrayMidPoint = (bitArrayLength / 2);

    // start the reversal
    for (int i = 0; i < bitArrayMidPoint; i++)
    {
        bool bit = myBitArray[i];
        myBitArray[i] = myBitArray[bitArrayLength - i - 1];
        myBitArray[bitArrayLength - i - 1] = bit;
    }

    // back to a byte
    singleElementArray = new byte[1];
    myBitArray.CopyTo(singleElementArray, 0);

    // return the reversed value
    return singleElementArray[0];
}

Here is my sample output running in the console window.

Answers


As suggested by other users, here is the fix I used that worked for me.

static void Main(string[] args)
{
    BitArray reversedBitArray = null;
    String bitString = String.Empty;
    byte reversedBits = 0;
    byte bits = 0;

    // prompt the user to enter a string of bits
    Console.WriteLine("Enter a string of bits here: ");

    // wait for the user to enter the string of bits
    bitString = Console.ReadLine();

    // convert the string of bits into an integer
    bits = Convert.ToByte(bitString, 2);

    // reverse all of the bits
    reversedBits = ReverseBits(bits);

    // display the reversed bits
    Console.WriteLine("\nThe reversed bit string is...");
    reversedBitArray = new BitArray(new byte[] { reversedBits });
    for (int i = (reversedBitArray.Length - 1); i > 0; i--)
    {
        Console.Write(Convert.ToInt32(reversedBitArray[i]));
    }

    // wait for the user to exit
    Console.WriteLine("\n\nPress any key to exit...");
    Console.ReadKey();

    // exit the console application
    Environment.Exit(0);
}

This simply prints the BitArray's values in the reverse order. Here is the new sample output.


Need Your Help

Wierd behaviour inside thread function

c multithreading network-programming

I have a server program written in C with a thread function join_handler. When i invoke this function in main- nothing after the statement -- printf("inside join handler %d",newsock); executes insi...

How do I get Mercury Editor to redirect to a new resource?

javascript ruby-on-rails mercury-editor

After watching RailsCast #296 about Mercury Editor, I am trying to get the editor to redirect to a newly created resource.