Solved

# Convert byte to bit array

Posted on 2004-11-02
17,109 Views
Hi all,
i have a vallue in byte and would like to convert it into bit array
example

byte b = 3;
char[] b = ConvertToBit(b)

now b will have
b = 00000011

any ideas how to do this in C# ?
thanks
0
Question by:Shay050799

LVL 48

Expert Comment

0

LVL 3

Author Comment

don't work for me..any other ideas ?
0

LVL 96

Accepted Solution

byte b = 5;
System.Collections.Specialized.BitVector32 bv = new System.Collections.Specialized.BitVector32(b);

Bob
0

LVL 20

Assisted Solution

BitArray bits = new BitArray (new byte[] {b});

Now you can check/set the bits using:

bits[0] = 1;
byte b0 = bits[0];

P.S. You need using System.Collections in the beginning of your file.
0

LVL 96

Expert Comment

I favor the BitVector32 over the BitArray for its efficiency.  If I need more than 32 bits, then I use the BitArray class.

Bob
0

Expert Comment

BitArray ba = new BitArray(8);
ba[3] = true;
byte[] bar = new byte[ba.Length / 8];
ba.CopyTo(bar, 0); // bit array to byte array
BitArray backtobitarray = new BitArray(bar); // byte array to bit array
0

## Featured Post

In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…