#include<stdio.h>
int main()
{
char a = -1;//char类型 8个bite位
//原码10000000000000001
//反码11111111111111110
//补码11111111111111111
//
//11111111
//111111111111111111111
//111111111111111111110
//100000000000000000001
//
signed char b = -1;
unsigned char c = -1;
//原码10000000000000001
//反码11111111111111110
//补码11111111111111111
//
//11111111 - c
//无符号数高位直接补0
//000000000000011111111
//
printf("%d %d %d ", a, b, c);
//打印a得整形提升,补充最高位(符号位)
//打印的是有符号数
return 0;
}
#include<stdio.h>
int main()
{
int a = -129;
unsigned int b = -129;
printf("%d %d\n", a, b);//强制打印有符号数
printf("%u %u\n", a, b);//强制打印无符号数
return 0;
}
#include<stdio.h>
//该编译器采用算术右移 右丢左补原符号位
int main()
{
int b = 5;
//正整数 三码相同
//00000000000000000000101 原码
//00000000000000000000101 反码
//00000000000000000000101 补码
b = b >> 1;
//00000000000000000000101
//00000000000000000000010 -> 2 = 0*2^0 + 1*2^1
printf("%d\n", b);
b = b << 1;
//00000000000000000000010
//00000000000000000000100 -> 4 = 0*2^0 + 0*2^1 + 1*2^2
printf("%d\n", b);
int c = -5;
//左边第一个数决定符号 1为负 0为正
//10000000000000000000101 原码
//11111111111111111111010 反码
//11111111111111111111011 补码 = 反码 + 1
c = c >> 1;
//11111111111111111111011 拿补码进行移动
//11111111111111111111101 新补码
//11111111111111111111100 新反码
//10000000000000000000011 -> -3
printf("%d\n", c);
c = c << 1;
//11111111111111111111101
//11111111111111111111010
//11111111111111111111001
//10000000000000000000110
printf("%d\n", c);
int d = 3;
int e = -5;
int f = d & e;//补码中同时为1才取1,其他都是0
//00000000000000000000000000011 3的补码
//11111111111111111111111111011 -5的补码
//00000000000000000000000000011
printf("%d\n", f);
int g = d | e;//补码中同时为0才取0,其他都是1
//00000000000000000000000000011 3的补码
//11111111111111111111111111011 -5的补码
//11111111111111111111111111011 新补码
//11111111111111111111111111010
//10000000000000000000000000101
printf("%d\n", g);
int h = d ^ e;//补码中相同为0,相异为1
//00000000000000000000000000011 3的补码
//11111111111111111111111111011 -5的补码
//11111111111111111111111111000 新补码
//11111111111111111111111110111
//10000000000000000000000001000
printf("%d\n", h);
//a ^ a = 0
//0 ^ a = a
}
小端字节序: 低位放在低地址
大端字节序:高位放在低地址