• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18427
  • Last Modified:

Convert byte to bit array

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
Shay050799
Asked:
Shay050799
2 Solutions
 
Shay050799Author Commented:
don't work for me..any other ideas ?
0
 
Bob LearnedCommented:
byte b = 5;
System.Collections.Specialized.BitVector32 bv = new System.Collections.Specialized.BitVector32(b);

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TheAvengerCommented:
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
 
Bob LearnedCommented:
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
 
bookbyteCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now