【小练习】程序设计基本概念:类型转换3

1.练习代码

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int func(int x);

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned char a=0xA5;
	unsigned char b=~a>>4+1;

	printf("b=%d\n", b);
	return 0;
}

2.关键点分析

2.1转换过程

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int func(int x);

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned char a=0xA5;
	unsigned char b=~a>>4+1;
	//运算顺序1:由于4为整型,会先把字符型的a提升为整型,即0x00A5。
	//运算顺序2:执行~a取反,即将0000 0000 1010 0101取反得到1111 1111 0101 1010
	//运算顺序3:4+1等于5
	//运算顺序4:右移5位,得到0000 0111 1111 1010,为250

	printf("b=%d\n", b);
	return 0;
}

2.2运行结果

【小练习】程序设计基本概念:类型转换3