I have two hex strings of 8 digits. I need to apply and operation for these two hex string, then apply right shift to 7 bits and get the decimal value. I have tried converting Hex strings to byte array of length 4 (8 *2 = 32 bits = 4 bytes) and did & operation to individual bytes in same order, saved the result to another byre array of length 4. How to do bit shifting to this byte array?
Ex : data1 in hex: 0x40003019,
data1 in bits: 0100-0000 0000-0000 0011-0000 0001-1001,
data1 in bytes: 64 0 48 25,
data2 in hex: 0x00FFFF80,
data2 in bits : 0000-0000 1111-1111 1111-1111 1000-0000,
data2 in bytes : 0 255 255 128
AND operation between data1Bytes , data2Bytes which gives output : bytearray1[0,0,48,0] (bits for these 0000-0000 0000-0000 0011-0000 0000-0000 and decimal value is 12,288).
Till this step all my conversions and calculations are working as expected. now I need to right shift 7 bits of this end result which should give 0000-0000 0000-0000 0000-0000 0110-0000( decimal value of 96).
1)I have tried converting byte array to int and apply right shift
var res = BitConverter.ToInt32(bytearray1, 0);
var shift = res >> 7;
but res = 3145728(which should be 12,228) and shift = 24,576(which should be 96)
2)I have tired converting bytearray1[0,0,48,0] into BitArray but bits in resultant BitArray are in reverse order
var bitArray = new BitArray(bytearray1);
bitArray[0]...bitArray[19] = false, bitArray[20] = bitArray[21] = true , bitArray[22]...bitArray[31] = false.
bitArray[0] -----------[31] : 0000 0000 0000 0000 0000 1100 0000 0000,
bit shifting this result wrong value. Please help me with this, what I am missing?
>>
with<<
... – Matthew Watsonnow I need to right shift 7 bits of this end result which should give 0000-0000 0000-0000 0000-0000 **0110-0000**
which, given that the previous step had0000-0000 0000-0000 **0011-0000** 0000-0000
, is a left shift. – Matthew Watson