user-0 提问: 关于有符号数的左移和右移的问题。

回复:
user: user-1
content: 左移与符号无关，都是逻辑左移（对应SHL指令），而右移无符号数做逻辑右移（对应SHR指令）、有符号数做算术右移（对应SAR指令）。 不过这也是与语言相关的，在Pascal中，移位一律是逻辑移位，算术移位要用乘除。

user: user-2
content: 仅供参考：#include <stdio.h> unsigned short int ui;   signed short int si; int main() {     ui=(unsigned short int)0x8000u;     si=(  signed short int)0x8000;     printf("ui=%u\n",ui);     printf("si=%d\n",si);     ui=ui>>1;     si=si>>1;     printf("ui=%u\n",ui);     printf("si=%d\n",si);      printf("--------------\n");     ui=(unsigned short int)0x8000u;     si=(  signed short int)0x8000;     printf("ui=%u\n",ui);     printf("si=%d\n",si);     ui=((  signed short int)ui)>>1;     si=((unsigned short int)si)>>1;     printf("ui=%u\n",ui);     printf("si=%d\n",si);      return 0; } //ui=32768 //si=-32768 //ui=16384 //si=-16384 //-------------- //ui=32768 //si=-32768 //ui=49152 //si=16384

user: user-3
content: 无符型是对的, 主要是在有符型, 左移显然是有问题的, 因为操作数是负数就会丢掉符号位, 而右移不是未定义,而是由实现定义, 差不多编译器都是算术右移,也满足这种结果.

user: user-0
content: 引用 2 楼 mymtom 的回复:有符号的整数右移的结果是不确定的  3.3.7 Bitwise shift operators  Syntax            shift-expression:                   additive-expression                   shift-expression <<  additive-expression                   shift-expression >>  additive-expression  Constraints     Each of the operands shall have integral type.    Semantics     The integral promotions are performed on each of the operands.  The type of the result is that of the promoted left operand.  If the value of the right operand is negative or is greater than or equal to the width in bits of the promoted left operand, the behavior is undefined.     The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros.  If E1 has an unsigned type, the value of the result is E1 multiplied by the quantity, 2 raised to the power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long, UINT_MAX+1 otherwise.  (The constants ULONG_MAX and UINT_MAX are defined in the header <limits.h> .)     The result of E1 >> E2 is E1 right-shifted E2 bit positions.  If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 divided by the quantity, 2 raised to the power E2 .  If E1 has a signed type and a negative value, the resulting value is implementation-defined.  也就是说，只有无符号数 左移和右移才相当于乘以或除以2的幂，有符号数的左移和右移与乘以、除以2的幂是无关的，这样对吗

user: user-4
content: 有符号的整数右移的结果是不确定的  3.3.7 Bitwise shift operators  Syntax            shift-expression:                   additive-expression                   shift-expression <<  additive-expression                   shift-expression >>  additive-expression  Constraints     Each of the operands shall have integral type.    Semantics     The integral promotions are performed on each of the operands.  The type of the result is that of the promoted left operand.  If the value of the right operand is negative or is greater than or equal to the width in bits of the promoted left operand, the behavior is undefined.     The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros.  If E1 has an unsigned type, the value of the result is E1 multiplied by the quantity, 2 raised to the power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long, UINT_MAX+1 otherwise.  (The constants ULONG_MAX and UINT_MAX are defined in the header <limits.h> .)     The result of E1 >> E2 is E1 right-shifted E2 bit positions.  If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 divided by the quantity, 2 raised to the power E2 .  If E1 has a signed type and a negative value, the resulting value is implementation-defined.

user: user-5
content: 楼上正解....

user: user-6
content: 假如左移后符号位变了怎么办  变了就变了，没有什么特别的，即由负数变成一正数。