I grabbed a piece of code from the internet (https://github.com/Cinegy/TsDecoder) and I'm trying to understand with it is doing.
This is the constructor:
public SatelliteDeliverySystemDescriptor(byte[] stream, int start)
{
Frequency =
$"{(stream[start + 2] >> 4) & 0x0F}{stream[start + 2] & 0x0F}{(stream[start + 3] >> 4) & 0x0F}{stream[start + 3] & 0x0F}{(stream[start + 4] >> 4) & 0x0F}{stream[start + 4] & 0x0F}{(stream[start + 5] >> 4) & 0x0F}{stream[start + 5] & 0x0F}";
OrbitalPosition =
$"{(stream[start + 6] >> 4) & 0x0F}{stream[start + 6] & 0x0F}{(stream[start + 7] >> 4) & 0x0F}{stream[start + 7] & 0x0F}";
WestEastFlag = ((stream[start + 8] >> 7) & 0x01) == 0x01;
Polarization = (byte)((stream[start + 8] >> 5) & 0x03);
RollOff = (byte)((stream[start + 8] >> 3) & 0x03);
ModulationSystem = ((stream[start + 8] >> 2) & 0x01) == 0x01;
Modulation = (byte)(stream[start + 8] & 0x03);
SymbolRate =
$"{(stream[start + 9] >> 4) & 0x0F}{stream[start + 9] & 0x0F}{(stream[start + 10] >> 4) & 0x0F}{stream[start + 10] & 0x0F}{(stream[start + 11] >> 4) & 0x0F}{stream[start + 11] & 0x0F}{(stream[start + 12] >> 4) & 0x0F}";
FECInner = (byte)(stream[start + 12] & 0x0F);
}
What i did
var hexString = "430B1200000025E0C610500003";
var hexBytes = Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
var descriptor = new SatelliteDeliverySystemDescriptor(hexBytes, 0);
NOTE: "hexBytes" is:
[0]: 67
[1]: 11
[2]: 18
[3]: 0
[4]: 0
[5]: 0
[6]: 37
[7]: 224
[8]: 198
[9]: 16
[10]: 80
[11]: 0
[12]: 3
This is the output:
Frequency = 12000000
OrbitalPosition = 25140
WestEastFlag = true
Polarization = 2
RollOff = 0
ModulationSystem = true
Modulation = 2
SymbolRate = 1050000
FECInner = 3
My question(s)
Why is the programmer doing e.g. this in order to get the result? What is the approch? Why he is doing all these shiftings AND BITwise AND operators?
$"{(stream[start + 2] >> 4) & 0x0F}{stream[start + 2] & 0x0F}{(stream[start + 3] >> 4) & 0x0F}{stream[start + 3] & 0x0F}{(stream[start + 4] >> 4) & 0x0F}{stream[start + 4] & 0x0F}{(stream[start + 5] >> 4) & 0x0F}{stream[start + 5] & 0x0F}";
In order words: What is exactly happening in this piece of code and is there not a better readable approach?
Thanks!
I grabbed a piece of code from the internet and I'm trying to understand with it is doing.At least point us to where you got it from, so we can check the context of what it is trying to achieve.