Click here to Skip to main content
15,742,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I spent long time understanding how programmers use the advantage of speedy binary representation of data in C# arrays but I just dont get it.

for example If I have 2 arrays A, and B to store 0/1 data. In normal situation we do like this :

bool flag=true;
 int[] A = new int[10] { 1, 0, 0, 0, 1, 1, 0, 1, 0, 0 };
 int[] B = new int[10] { 1, 1, 1, 0, 0, 1, 0, 1, 0, 0 };
 // For comparing the first 5 bits :
 for(int i=0;i<5;i++)
     if (A[i] != B[i])
         flag = false;
 // Accessing the i*th* positions is :
int x=B[5];

What if I need to repeat this code for thousands of times ? or even the arrays are very large ? the straight answer will be to represent the data as bit packed arrays and apply things like bitwise operations or bitmasking ... etc.

My question is how to switch to the binary world in c# ? for more accurate question, How to rewrite the code above in very efficient way using the binary representation and bit wise operations ? Answer with demo code will be much appreciated.
Updated 22-Jul-15 20:29pm
[no name] 23-Jul-15 4:40am    
What makes you think bit manipulation has anything to do with fast array processing? Bit manipulation applies to integers.
Member 11856803 23-Jul-15 5:29am    
processing batch of elements is much faster than processing one element by time
[no name] 23-Jul-15 5:32am    
Please provide the example of how you "switch to the binary world in C++" to achieve fast array processing. There are actually quite a few programmers here.

I would use the BitArray class[^].
Share this answer
Member 11856803 23-Jul-15 4:12am    
is there anyone in this website really a programmer ?
CPallini 23-Jul-15 4:42am    
Don't be silly.
If you need a practical way of handling bits then your best bet is the suggested class.
If you just need to make some practice with bit manipulation then you should learn how to do it.
Finally, if you need to outperform the provided .NET framework class then you have probably to go to native level.
Member 11856803 23-Jul-15 5:14am    
BitArray is very slow , Bool is faster. Dealing with 0/1 data better to be represented using int by bit manipulation in each int element.
However my problem is that I don't know how to do that in code. I am basically a c++ programmer. I was looking for a good programmer who can transform the attached code according to my question details so I can learn
I couldnt find good resource to go with !
[no name] 23-Jul-15 5:16am    
Why not provide a c++ example to explain.
CPallini 23-Jul-15 6:11am    
Indexing an array is faster than the required shift & mask even using C++. There is a trade off between speed a space efficiency, you know.
Did you test the bit BitArray class in order to claim its slowness?
Have you had a look at C++ std::bitset class performance?
Your fundamental question is how to switch to the binary world in C#! And the answer is once you know the theory of Boolean Algebra[^] it would be like a peiece of cake! Deep down its all mathematics, seeing your code it seems like you have not reviewed these concepts.

Once you understand the theory, rest is easy. Check CPallini solution for Bit Array implementation in C#
Share this answer
Member 11856803 23-Jul-15 4:11am    
Asif your answer is very ridiculous if I may. There is a code if you can turn in it or not into binary based implementation this is the question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900