2

!!!!ANSWERED!!!!

I need little help finishing my simple bit manipulation program.

  1. Asks the user for a number
  2. Print the number's binary representation
  3. Ask the user for bit position and bit value
  4. Apply the changes
  5. Displays the changes made by the user in binary
  6. ?????? Convert that changes into a number again. ??????????

I just need a little help with step 5, as you will see from my code I'm a complete noob, so please do not laugh :) Any help will be deeply appreciated.

Console.WriteLine("Enter integer number");
int number = Convert.ToInt32(Console.ReadLine());
string binaryString = Convert.ToString(number, 2);
Console.WriteLine("The binary representation of {0} is", number);
Console.WriteLine(binaryString.PadLeft(16, '0'));


BitArray b = new BitArray(new int[] { number }); 


Console.WriteLine("Enter bit's position (0 to 15)");
int position = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter bit's value (true or false)");
bool value = Convert.ToBoolean(Console.ReadLine());

b.Set(position, value); //set value to given position based on input from the user

Console.WriteLine("Your changes transformed \n{0} \nto ",binaryString.PadLeft(16,'0'));

for (int i = 15; i >= 0; i--) 
{
    if (b[i] == true)
    {
        Console.Write(1);
    }
    else
    {
        Console.Write(0);
    }
}
Console.WriteLine();
4
  • this will help you. stackoverflow.com/questions/5283180/… Commented Mar 21, 2014 at 10:11
  • not clear what are you asking? please localize the question, and ask only the part you want to ask Commented Mar 21, 2014 at 10:12
  • Thank you all , I found the solution immediately after asking the question here I was trying to complete it for a whole day this is what works for me and I will try bansi's suggestion as well int[] array = new int[1]; b.CopyTo(array, 0); Console.WriteLine(array[0]); Commented Mar 21, 2014 at 10:13
  • 1
    If you have a correct answer, you should consider adding it as an answer, and marking that as the accepted answer instead of adding "answered!!" in your OP... Commented Mar 21, 2014 at 10:34

3 Answers 3

2

Convert.ToInt32 has an overload that takes the base used for the string representation of your integer

 Console.WriteLine(Convert.ToInt32(binaryString, 2))

However, after you have changed the bits inside the BitArray, to use the Convert.ToInt32 you need something to reconvert your bit array to a string

This code is adapted from another answer here on SO

.....
binaryString = ToBitString(b);
Console.WriteLine(Convert.ToInt32(binaryString, 2))
.....


public string ToBitString(BitArray bits)
{
    var sb = new StringBuilder();
    for (int i = bits.Count - 1; i>= 0; i--)
    {
        char c = bits[i] ? '1' : '0';
        sb.Append(c);
    }
    return sb.ToString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you all I will look into your suggestions in order the understand the logic behind it :)
0

Store the binary representation into a variable:

string binary = binaryString.PadLeft(16, '0');
Console.WriteLine(binary);

Then get the position and value:

 Console.WriteLine("Enter bit's position (0 to 15)");
 int position = Convert.ToInt32(Console.ReadLine());

 Console.WriteLine("Enter bit's value (true or false)");
 bool value = Convert.ToBoolean(Console.ReadLine());

Make the changes:

var chars = binary.ToCharArray();
chars[position] = value ? '1' : '0';
binary = new string(chars);

Display the new binary representation and it's decimal equivelant:

Console.WriteLine("New binary value: {0}",binary);
Console.WriteLine("New decimal value: {0}", Convert.ToInt32(binary, 2));

Comments

0

Change the final loop to the following:

int v = 0;
for (int i = 15; i >= 0; i--)
{
   if (b[i] == true)
   {
       Console.Write(1);
       v += (int)Math.Pow(2, i);
   }
   else
   {
       Console.Write(0);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.