- #include <stdio.h>
- #define MSB 0x80
- #define LSB 0x01
- // 数组数据整体按位左移一位
- int left_shift(unsigned char *str, int len)
- {
- int i;
- for(i = 1; i <= len; i++)
- {
- str[i-1] = str[i-1] << 1;
- if(i < len && str[i] & MSB)
- {
- str[i-1] = str[i-1] | LSB;
- }
- }
- return 0;
- }
- // 数组数据整体按位右移一位
- int right_shift(unsigned char *str, int len)
- {
- int i;
- for(i = len-1; i >= 0; i--)
- {
- str[i] = str[i] >> 1;
- if(i > 0 && str[i-1] & LSB)
- {
- str[i] = str[i] | MSB;
- }
- }
- return 0;
- }
- int main(void)
- {
- int i;
- unsigned char x1[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
- unsigned char x2[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
- left_shift(x1, sizeof(x1)/sizeof(x1[0]));
- right_shift(x2, sizeof(x2)/sizeof(x2[0]));
- for(i = 0; i < sizeof(x1)/sizeof(x1[0]); i++)
- printf("%02x ", x1[i]);
- printf("\n");
- for(i = 0; i < sizeof(x2)/sizeof(x2[0]); i++)
- printf("%02x ", x2[i]);
- return 0;
- }