Convert byte to bit array

Posted on 2004-11-02
Last Modified: 2011-09-20
Hi all,
i have a vallue in byte and would like to convert it into bit array

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

now b will have
b = 00000011

any ideas how to do this in C# ?
Question by:Shay050799
    LVL 48

    Expert Comment

    LVL 3

    Author Comment

    don't work for me..any other ideas ?
    LVL 96

    Accepted Solution

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

    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.
    LVL 96

    Expert Comment

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


    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    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 ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now