0

I have a 16-byte array, and am trying to left-shift the number of bits - where the number to shift can vary from 1 to 10. I tried the code below, but it only works with a 4 bit shift:

Rx_Table[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0A, 0xBC};
if (byteezy == 0x00 && byteezy1 == 0x10) {
    int i;
    uint8_t shifted = 0x00;
    uint8_t overflow = (0xF0 & RxTable[0]) >> 4;

    for (i = (16 - 1); i >= 0; i--) {
        shifted = (RxTable[i] << 4) | overflow;
        overflow = (0xF0 & RxTable[i]) >> 4;
        RxTable1[i] = shifted;
    }

I am stuck: how to make this work when the number of bits to shift is variable?

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
kooda
  • 11
  • 8
  • What about `__int128 rx; memmove(&rx,Rx_Table, 16); rx <<= 10; memmove(Rx_Table,&rx, 16); ` – Victor Gubin Jul 03 '18 at 09:41
  • 1
    Nobody can answer the question without knowing the type of Rx_Table. – Lundin Jul 03 '18 at 10:47
  • Have you tried to replace every occurrence of "4" with a variable? You should be able to get an example that works with shifts up to 8. The overflow calculation is using a mask 0xF0 that needs to be calculated. Some of the shifts are the not the amount you want to shift but the remaining number of bits in the byte. Unfortunately for 4 bits both of these are 4.I would start with a test pattern with the last byte set as 0xFF and debug that. Get the last byte correct. Then, with the same test pattern, get the second byte correct. Stick with 0-8 shifts until it is working before doing higher. – William J Bagshaw Jul 03 '18 at 11:25
  • Actually I'm not convinced you need the 0xF0 mask. A common gotcha (not fixed by adding the mask) is using signed values. Shifting a negative number may keep it negative. It could be "sign extended". Make sure the type you use for the array is unsigned. – William J Bagshaw Jul 03 '18 at 11:33
  • You defined Rx_Table[16], but only initialized 10 entries. By shift left, do you mean Rx_Table[0]->Rx_Table[16] or Rx_Table[16]->Rx_Table[0]? – Marker Jul 03 '18 at 12:31

0 Answers0