c语言中的右移是逻辑右移还是算术右移的问题

来源:互联网 发布:js 替换换行符 编辑:程序博客网 时间:2024/05/16 17:42

    先上代码

    // 10191.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"


#include<stdio.h>
int main()
{
char x=0xfe;
int y=x>>1;
printf("%d",y);
}

x是有符号类型,x=1111 1110

那么x>>1=1111 1111

由于是有符号类型的数据,所以y=-1

那么在c语言中,右移是算术右移


// 10192.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"


#include<stdio.h>
int main()
{
char x=0x0e;
int y=x>>1;
printf("%d",y);
}

x=0xe=0000 1110

那么x>>1=0000 0111=7


0 0