开发者

How to circular shift an array of 4 chars?

开发者 https://www.devze.com 2023-01-20 10:09 出处:网络
I have an array of four unsigned chars. I want to treat it like a 32-bit number (assume the upper bits of the char are don\'t care. I only care about the lower 8-bits). Then, I want to circularly shif

I have an array of four unsigned chars. I want to treat it like a 32-bit number (assume the upper bits of the char are don't care. I only care about the lower 8-bits). Then, I want to circularly shift it by an arbitrary number of places. I've got a few different shift sizes, all determined at compile-time.

E.g.

unsigned char a[4] = {0x81, 0x1, 0x1, 0x2};
circular_left_shift(a, 1);
/* a is now { 0x2, 0x2, 0x2, 0x5 } */

Edit: To everyone wondering why I didn't mention CHAR_BIT != 8, because this is standard C. I didn't specify a platform, so why are you assum开发者_如何转开发ing one?


static void rotate_left(uint8_t *d, uint8_t *s, uint8_t bits)
{
   const uint8_t octetshifts = bits / 8;
   const uint8_t bitshift = bits % 8;
   const uint8_t bitsleft = (8 - bitshift);
   const uint8_t lm = (1 << bitshift) - 1;
   const uint8_t um = ~lm;
   int i;

   for (i = 0; i < 4; i++)
   {
       d[(i + 4 - octetshifts) % 4] =
           ((s[i] << bitshift) & um) | 
           ((s[(i + 1) % 4] >> bitsleft) & lm);
   }
}   

Obviously


While keeping in mind plain C the best way is

inline void circular_left_shift(char *chars, short shift) {
    __int32 *dword = (__int32 *)chars;
    *dword = (*dword << shift) | (*dword >> (32 - shift));
}

Uhmm, char is 16 bits long, was not clear for me. I presume int is still 32 bit.

inline void circular_left_shift(char *chars, short shift) {
    int i, part;
    part = chars[0] >> (16 - shift);
    for (i = 0; i < 3; ++i)
        chars[i] = (chars[i] << shift) | (chars[i + 1] >> (16 - shift));
    chars[3] = (chars[3] << shift) | part;
}

Or you could just unwind this cycle.

You could dig further into asm instruction ror, on x86 it's capable of performing such shift up to 31 bits left. Something like a

MOV CL, 31
ROR EAX, CL


Use union:

typedef union chr_int{
   unsigned int  i;
   unsigned char c[4];
};

It's safer (because of pointer aliasing) and easier to manipulate.

EDIT: you should have mention earlier that your char isn't 8 bits. However, this should do the trick:

#define ORIG_MASK 0x81010102
#define LS_CNT 1

unsigned char a[4] = { 
    ((ORIG_MASK <<  LS_CNT      ) | (ORIG_MASK >> (32 - LS_CNT))) & 0xff,
    ((ORIG_MASK << (LS_CNT +  8)) | (ORIG_MASK >> (24 - LS_CNT))) & 0xff,
    ((ORIG_MASK <<  LS_CNT + 16)) | (ORIG_MASK >> (16 - LS_CNT))) & 0xff,
    ((ORIG_MASK << (LS_CNT + 24)) | (ORIG_MASK >> ( 8 - LS_CNT))) & 0xff
};
0

精彩评论

暂无评论...
验证码 换一张
取 消