Manjunath ST Manjunath ST - 8 months ago 68
C# Question

How to get bit values from Byte?

I have a byte say 1 byte of size of 8.. How do i get the bit each values? for example, I want 16th bit value, 17th bit, 18th bit so on..

byte[] _byte = new byte[8];


If you want the Xth bit in your Byte Array (I think that is what your asking at least), you need to index the correct Byte from the array and then extract the bit

public static Boolean GetBitX(byte[] bytes, int x) {
    var index = x/8;
    var bit = x-index*8;

    return (bytes[index] & (1<<bit)) != 0;