首页 > 学院 > 开发设计 > 正文

算术/逻辑移位的实现。

2019-11-10 19:13:24
字体:
来源:转载
供稿:网友
/*.c文件声明:算术/逻辑移位的实现。C语言的移位要取决于数据类型:对于无符号数,左移时右侧补0(相当于逻辑移位)对于无符号数,右移时左侧补0(相当于逻辑移位)对于有符号数,左移时右侧补0(叫算术移位,相当于逻辑移位)对于有符号数,右移时左侧补符号位(如果正数就补0,负数就补1,叫算术移位),原来符号位的值会被移到下一位!PRintf中的%d表示: 十进制整数。由下例可知,它可以自动识别该数是有符号数还是无符号数。*/#include<stdio.h>int main(void){short signed_bit_8 = 0xffff;//直接将该变量塞爆,无报错。直接写该内存空间。unsigned short unsigned_bit_8 = 0xffff;printf("signed_bit_8 = %d/n",signed_bit_8);//out_put = -1printf("unsigned_bit_8 = %d/n",unsigned_bit_8);//out_put = 65535signed_bit_8 = signed_bit_8 << 1;unsigned_bit_8 = unsigned_bit_8 << 1;printf("signed_bit_8 = %d/n",signed_bit_8);//out_put = -2printf("unsigned_bit_8 = %d/n",unsigned_bit_8);//out_put = 65534signed_bit_8 = 0xffff;unsigned_bit_8 = 0xffff;signed_bit_8 = signed_bit_8 >> 1;unsigned_bit_8 = unsigned_bit_8 >> 1;printf("signed_bit_8 = %d/n",signed_bit_8);//out_put = -1printf("unsigned_bit_8 = %d/n",unsigned_bit_8);//out_put = 32767return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表