简体   繁体   English

C ++中的XOR运算符和char

[英]XOR operator and char in C++

Given the following code fragment in C++: 在C ++中给出以下代码片段:

char a = 0x0a;
char b = 0x80;
char c = a ^ b;

what will happen behind the screen? 屏幕后面会发生什么? When I use cout to output c, there was no output. 当我使用cout输出c时,没有输出。 Will the char a and char b be converted to binary format? 将char a和char b转换为二进制格式吗? what's the format of c? c的格式是什么?

c will be 0x8a, which is usually not a printable character, see (eg) http://www.unicodetools.com/unicode/img/latin-iso-8859-1.gif c为0x8a,通常不是可打印字符,请参见(例如) http://www.unicodetools.com/unicode/img/latin-iso-8859-1.gif

(neither are 0x0a (line feed) or 0x80) (都不是0x0a(换行)或0x80)

cout will print the character corresponding to the value of c for char arguments, that's why you don't see any output. cout将为char参数打印与c的值相对应的字符,这就是为什么看不到任何输出的原因。

You may want to use printf to print the numeric value of c, or cast c to an int. 您可能要使用printf打印c的数值,或将c强制转换为int。

You may also be interested in this question, which discusses printing binary numbers in C: Is there a printf converter to print in binary format? 您可能也对此问题感兴趣,该问题讨论了用C打印二进制数: 是否有一个printf转换器可以二进制格式打印?

All numbers in a computer are in binary format. 计算机中的所有数字均为二进制格式。

0x0a , aka 10 (ten), is represented as 0x0a (又名10 (十))表示为

1010 1010

and 0x80 , aka 128 , is represented as 0x80 (又名128 )表示为

10000000 10000000

If you xor those, you get 138. 如果对它们进行xor,则得到138。

Apparently, your terminal doesn't output anything useful if you print the char 138. 显然,如果您打印char 138,则终端不会输出任何有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM