Post by John SmithWhat does the ^ (circumflex) operator do in C?
int a = 100, b = 10;
int c = a ^ b;
printf("c == %d\n", c);
produces the output: c == 110
What's going on here besides addition?
The ^ (circumflex) operator in C is bit-wise exclusive
OR (a.k.a. XOR). Here is the truth table:
A B A XOR B
---------------
0 0 0
0 1 1
1 0 1
1 1 0
So in your example, converting to binary:
A: 0 1 1 0 0 1 0 0 (64 hex, 100 decimal)
B: 0 0 0 0 1 0 1 0 (0A hex, 10 decimal)
--------------------
0 1 1 0 1 1 1 0 (6E hex, 110 decimal)
This is NOT addition.
You may want to search the web for "Boolean Arithmetic".
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book