Can someone tell how this method works?

This method allows you I understand translates bytes to bits
private BitArray ByteToBit(byte src) {
 BitArray bitArray = new BitArray(8); 
 bool st = false;
 for (int i = 0; i < 8; i++) {
 if ((src >> i & 1) == 1) {
 st = true;
}
 else st = false;
 bitArray[i] = st;
}
 return bitArray;
 }
March 23rd 20 at 19:16
1 answer
March 23rd 20 at 19:18
Converts a number (a byte) into an array of bits. Because of the number 13 in binary notation 1101 will [1, 0, 1, 1, 0, 0, 0, 0]
private byte BitToByte(BitArray scr)
{
 byte num = 0;
 for (int i = 0; i < scr.Count; i++)
 if (scr[i] == true)
 num += (byte)Math.Pow(2, i);
 return num;
 }

And this also works only in reverse? or what? - loren commented on March 23rd 20 at 19:21
@Ona84, If this function really does, somehow wrong, because the array of bits for the number 13 should look like this [0,0,0,0,1,0,1,1]. - krystel_Mann24 commented on March 23rd 20 at 19:24
@loren, yeah, it's in the opposite direction transformation.
@krystel_Mann24, 13 = 8 + 4 + 1 = 1101b. This algorithm writes the bits starting with the lowest order in the array in order. It turns upside down the number. But not the fact that it's not right, it is a reasonable way to store. - Ona84 commented on March 23rd 20 at 19:27

Find more questions by tags C#