1.4 基本数据类型及表达式 【C++】


#include <iostream>
using namespace std;
#if 1
int main() {
	 unsigned char a = 0x80;   // 1000 0000
	 cout << "a(0x80):" << (int)a << endl;    //  128
	 a = ~a;     // 0111 1111      127
	 cout << "~a:" << (int)a << endl;

	 unsigned char c = 0x81;   // 1000 0001
	 unsigned char d = c & 0xfe;   // 使最低为变0,其他位不变       1000 0000

// 要使得cout输出十六进制,则需要让变量c从char类型变成int类型,再加上hex
	 cout << " c = 0x" << hex << (int)c << endl;          
	 cout << "before value of d:0x" << (int)d << endl;
 
//将第二位置为1,其余位不变
	 d = c | 0x02;        // 0000 0010
 	 cout << "after value of d:0x" << hex << (int)d << endl;    //  1000 0011
 }
 #endif

// 逻辑与&&    或||     非!
// 按位与&     或|      非~      异或^
发布了41 篇原创文章 · 获赞 1 · 访问量 482

猜你喜欢

转载自blog.csdn.net/weixin_44773006/article/details/103493118