天天看点

移位运算及其compiler-dependent

C中的移位操作,其实内容不太多,但是有一些小的细节,需要注意,以便碰到特定问题,我们能有思路去分析。

移位操作主要分为: Shift(Left, Right),Circular Shift(Left, Right)。从移位的性质看,主要分为:逻辑移位,算数移位。

一、算数移位 VS 逻辑移位

二者核心区别就是对符号位的处理,算术移位是考虑符号位的,逻辑移位不考虑符号位,直接进行移位。即算数移位:空缺位补符号位;逻辑移位:空缺位补0,忽略符号特性。

二、左移和右移的处理

下面用摘自 stackoverflow 的一段话来说明:

When shifting left, there is no difference between arithmetic and logical shift. When shifting right, the type of shift depends on the type of the value being shifted.

(As background for those readers unfamiliar with the difference, a "logical" right shift by 1 bit shifts all the bits to the right and fills in the leftmost bit with a 0. An "arithmetic" shift leaves the original value in the leftmost bit. The difference becomes important when dealing with negative numbers.)

Many C compilers choose which right shift to perform depending on what type of integer is being shifted; often signed integers are shifted using the arithmetic shift, and unsigned integers are shifted using the logical shift.

具体代码示例:

#include <stdio.h> 

void print_char(char x)  
{  
  unsigned char * bp=(unsigned char *)&x;  
  int size=sizeof(x);  
  for(int i=0; i<size; i++)  
  {
  	printf("%.2x", bp[i]);  
  }
  printf("\n");  
} 
 
int main(void)  
{  
    unsigned char x1 = 3;  //0000 0011
    signed char x2 = 3;   //0000 0011
    signed char x3 = -3;  //1111 1101

    print_char(x1 << 1);  //0000 0110
    print_char(x1 >> 1);  //0000 0001
    print_char(x2 << 1);  //0000 0110
    print_char(x2 >> 1);  //0000 0001
    print_char(x3 << 1);  //1111 1010
    print_char(x3 >> 1);  //1111 1110
 
    return 0;  
} 
           

说明:

1)将数据写成其机器表示(补码表示),然后直接按规则执行移位操作(左移逻辑移位,符号数右移依赖于编译器实现,一般算术移位,无符号数右移逻辑移位)。

2)注意到子函数 print_char,非常好的 hexadecimal 输出实现,注意体会下其思路。

需要补充说明的:

unsigned char * bp=(unsigned char *)&x;  //强转保证按照无符号数解析

printf("%.2x", bp[i]);   //输出无符号十六进制数,同时, the precision .2 specifies the minimum number of digits for an integer.

分别指定 %.1x %.2x %.5x,输出结果如下(gcc编译器):

移位运算及其compiler-dependent
移位运算及其compiler-dependent
移位运算及其compiler-dependent

三、循环移位(以下内容来自于WIKI)

1、移位过程

左循环移位:

移位运算及其compiler-dependent

右循环移位:

移位运算及其compiler-dependent

2、Implementing circular shifts

Circular shifts are used often in cryptography in order to permute bit sequences. Unfortunately, many programming languages, including C, do not have operators or standard functions for circular shifting, even though several processors have bitwise operation instructions for it (e.g. Intel x86 has ROL and ROR). However, some compilers may provide access to the processor instructions by means of intrinsic functions. In addition, it is possible to write standard ANSI C code that compiles down to the "rotate" assembly language instruction (on CPUs that have such an instruction). Most C compilers recognize this idiom:

unsigned int x;
  unsigned int y;
  /* ... */
  y = (x << shift) | (x >> (sizeof(x) * CHAR_BIT - shift));
      

and compile it to a single 32-bit rotate instruction.[1][2]

On some systems, this may be "#define"ed as a macro or defined as an inline function called something like "rightrotate32", "rotr32" or "ror32" in a standard header file like "bitops.h".[3] Rotates in the other direction may be "#define"ed as a macro or defined as an inline function called something like "leftrotate32", "rotl32" or "rol32" in the same "bitops.h" header file.

If necessary, circular shift functions can be defined (here in C):

/* We don't need special handling for certain shift values:
 * Shift operations in C are only defined for shift values which are
 * not negative and smaller than sizeof(value), but the compiler
 * will generate correct code for the following code pattern. */
 
unsigned int rotl(unsigned int value, int shift) {
    return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift));
}
 
unsigned int rotr(unsigned int value, int shift) {
    return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift));
}