关于有符号数的左移和右移的问题。

善良的小伙伴 2015-07-16 08:25:18
对于无符号数来说,左移1位,相当于乘以一个2,右移一位相当于除以一个2;
对于有符号数,先说右移:
普通的右移,都是在左边补0,那么对于一个负数最高位为1,又移后,左边符号位补0,这肯定是不对的,那么可以这样解决:对于有符号数,如果是正数,右移左边补0,如果是负数,右移左边补1,,这样可以解决。
再说左移,负数的左移要怎么解决呢?右边只能补0,假如左移后符号位变了怎么办。。


另外还有一个浮点数的问题,发在汇编区木有人解决。。。求解:
http://bbs.csdn.net/topics/391072171
...全文
3792 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
左移与符号无关,都是逻辑左移(对应SHL指令),而右移无符号数做逻辑右移(对应SHR指令)、有符号数做算术右移(对应SAR指令)。 不过这也是与语言相关的,在Pascal中,移位一律是逻辑移位,算术移位要用乘除。
赵4老师 2015-07-17
  • 打赏
  • 举报
回复
仅供参考:
#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
fly_dragon_fly 2015-07-17
  • 打赏
  • 举报
回复
无符型是对的, 主要是在有符型, 左移显然是有问题的, 因为操作数是负数就会丢掉符号位, 而右移不是未定义,而是由实现定义, 差不多编译器都是算术右移,也满足这种结果.
善良的小伙伴 2015-07-17
  • 打赏
  • 举报
回复
引用 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的幂是无关的,这样对吗
mymtom 2015-07-17
  • 打赏
  • 举报
回复
有符号的整数右移的结果是不确定的 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.
GKatHere 2015-07-17
  • 打赏
  • 举报
回复
楼上正解....
ForestDB 2015-07-16
  • 打赏
  • 举报
回复
假如左移后符号位变了怎么办 变了就变了,没有什么特别的,即由负数变成一正数。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧