1

I'm wondering if there are built in classes for working with binary numbers and converting any number (in this case decimal) to a binary and vice-versa. For example if I want to check if the third bit is 1 I can use the & bitwise operator but I feel it will be much more simple if there is indeed a way to convert the decimal to binary and check the value on the position I want. I've searched and find examples using BitArray but for some reason it throws me an error the it does not exist and I'm wondering if it's outdated. Also I tried MS library and found BitConverter.GetBytes(value) but I'm not sure what it does at all because when I try the example which is :

 int value = 12345678;
 byte[] bytes = BitConverter.GetBytes(value);
 Console.WriteLine(BitConverter.ToString(bytes));

The output is 4E-61-BC-00 which I'm not sure what actually is. What I'm looking for is if I have like int i = 247; // 11110111 and then:

i[0] = 1;
i[1] = 1;
i[2] = 1;
i[3] = 0;
i[4] = 1;
i[5] = 1;
i[6] = 1;
i[7] = 1;

So I can get 1 or 0 for any position.

i[2] = 1;
Florent
12.4k10 gold badges51 silver badges58 bronze badges
asked Sep 12, 2012 at 16:25
1
  • Integers are already in binary, it's when you want the decimal representation that they are converted. Commented Sep 12, 2012 at 16:50

2 Answers 2

3

You should use BitArray just make sure your added using System.Collections; to your CS file. You can pass a new BitArray a byte[], which will handle the conversion for you.

http://msdn.microsoft.com/en-us/library/x1xda43a.aspx

Jduv
1,0808 silver badges10 bronze badges
answered Sep 12, 2012 at 16:27

Comments

2

Well, yes, you can create a BitArray from an int:

int i = 247;
BitArray b = new BitArray(i);

And then you can check the third bit:

if (b[2])
{
 // the bit is set
}

The elements in the BitArray are Boolean, so to set the third bit, you'd write:

b[2] = true;

Converting back to an int is a bit of a pain, though. because you have to use CopyTo to copy the BitArray to an int[], and then access the first item in the int[].

I don't understand why you'd want to go through the headache. It's going to be a lot slower and more cumbersome than using the bitwise & operator. You could easily create constants for the different bit values:

const int bit0 = 0x01;
const int bit1 = 0x02;
const int bit2 = 0x04;
const int bit3 = 0x08;
const int bit4 = 0x10;
// etc.

And then:

if ((i & bit2) != 0)
{
 // the bit is set
}

Setting a single bit is:

i |= bit2;

Resetting a bit is:

i &= ~bit2;
answered Sep 12, 2012 at 16:33

5 Comments

There are plenty of reasons to want an abstraction of a bit-set data structure. Consider bit-vector algorithms (such as BDD or data flow analysis), and the BitArray object supports set opertaions. You are completely right in that it won't be as fast as native bit shifting, however.
I had the bitwise operator solution before asking the question, and for now, because I'm just learning C#.NET the reason to search for alternatives is for the sake of simplicity and finding new features in .NET. And it's easier at least for me to use BitArray.
@Jduv: I understand why you'd want an abstraction of bit-set data, but I wouldn't think it would be common when working with a single integer.
+1. @Leron, Keep it as learning exercise, but think twice to use it in real code. I think using BitArray as "simplification of bitwise operators on int/long" will confuse most readers of your code. And it is unlikely to be useful: if bit-packing used for performance reasons any conversion operation is suspicious, if it is just from some legacy/external source - use wrapper class that will expose necessary data in nice way and who cares what is inside of the methods.
It's just I've never been working on such a low level and the reason to try and learn more is because I may need it in future. Thus said, the abstraction is a lot more easier to understand than the work with bitwise operators even thou it seem that it's better. Do you think that I have to put more effort on it and try to escape BitArray and stick to the way Jim Mischel has shown..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.