0

Given the following code fragment in 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. Will the char a and char b be converted to binary format? what's the format of c?

Captain Rib
  • 65
  • 3
  • 6
  • What will happen is you'll get the bitwise exclusive or of those two numbers, what else? – juanchopanza Jan 10 '15 at 17:09
  • It's easy to find good explanation of this, for example in [wikipedia](http://en.wikipedia.org/wiki/Bitwise_operations_in_C). – tumdum Jan 10 '15 at 17:09
  • [This](http://img.c4learn.com/2012/03/Bitwise-XOR-Operator-in-Java-Programming-Language.png) happens. – BWG Jan 10 '15 at 17:10
  • @TomaszKłak I know exactly what is XOR operation. I was wondering why there is no output if I print c? Will a and b firstly be converted to binary then XOR? – Captain Rib Jan 10 '15 at 17:12
  • xor of those two numbers (0x8a) is most likely not printable character in your system. – tumdum Jan 10 '15 at 17:16

2 Answers2

5

c will be 0x8a, which is usually not a printable character, see (e.g) http://www.unicodetools.com/unicode/img/latin-iso-8859-1.gif

(neither are 0x0a (line feed) or 0x80)

cout will print the character corresponding to the value of c for char arguments, that's why you don't see any output.

You may want to use printf to print the numeric value of c, or cast c to an 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?

Community
  • 1
  • 1
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
2

All numbers in a computer are in binary format.

0x0a, a.k.a. 10 (ten), is represented as

1010

and 0x80, a.k.a. 128, is represented as

10000000

If you xor those, you get 138.

Apparently, your terminal doesn't output anything useful if you print the char 138.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82