1. 程式人生 > >c語言中的右移是邏輯右移還是算術右移的問題

c語言中的右移是邏輯右移還是算術右移的問題

    先上程式碼

    // 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