Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get a specific bit from a byte array.For example 37[2].

I have HistoryElements[37] but here i have to display the 2nd bit of 37th byte.

What I have tried:

I have used Bitconverter.GetBytes and Bitarray but it dint worked.
Posted
Updated 17-Feb-16 21:26pm

1 solution

Use BitArray class. e.g.
C#
byte[] b = new byte[37];
     for (int n = 0; n < b.Length; ++n)
       b[n] = (byte)n;

     // get bit 5 from 37th item
     System.Collections.BitArray ba = new System.Collections.BitArray(new byte[1]{b[36]});

     Console.WriteLine("item[36][5] is {0}", ba.Get(5));
 
Share this answer
 

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