c语言中移位操作符的初步使用举例

#include<stdio.h>
int main()
{    
    //左移操作符

    int a = 5;        //补码00000000 00000000 00000000 00000101    原码值为5
    int b = a << 1;    //补码00000000 00000000 00000000 00001010    原码值为10
    printf("%d\n", a);//位操作符后,a的值不变                    相当于  *2
    printf("%d\n", b);
    printf("\n");
    int c = -5;        //补码11111111 11111111 11111111 11111011    原码值为-5
    int d = c << 1;    //补码11111111 11111111 11111111 11110110    原码值为-10
                    //                                            相当于    *2
                    //此时检测符号位是否为1,为1怎按负数补码处理,为0则直接输出
                    //反码11111111 11111111 11111111 11110101
                    //原码10000000 00000000 00000000 00001010
    printf("%d\n", c);//打印出来的是原码
    printf("%d\n", d);
    printf("\n");
    int e = -2147483647;                                    //    原码值为-2147483647
//11111111 11111111 11111111 11111111原码
//10000000 00000000 00000000 00000000反码
//10000000 00000000 00000000 00000001补码
    int f = e << 1;                                            //    原码值为2
    printf("%d\n", e);//10000000 00000000 00000000 00000001补码
    printf("%d\n", f);//0000000 00000000 00000000 000000010补码
    printf("\n");
//左边丢弃,右边补零(丢了符号位)
    //-2147483647
//11111111 11111111 11111111 11111111原码
//10000000 00000000 00000000 00000000反码
//10000000 00000000 00000000 00000001补码


    //右移操作符
    int g = -5;        //11111111 11111111 11111111 11111011补码
    int h = g >> 1;    //11111111 11111111 11111111 11111101补码
                    //10000000 00000000 00000000 00000011原码
                    //此编译器为
                    //算数右移:符号位补原来的符号位
                    //如若为逻辑右移:
                    //符号位补零
    printf("%d\n", g);
    printf("%d\n", f);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值