` (6*6+6)^(2-9%4) = 43 ?`

who can explain to me what is exclusive bitwise OR `^`
 ``123`` ``````(6*6+6) = 42 (2-9%4) = 1 why the answer become 43 ??``````

and what is the differences between ` bitwise inclusive OR (|) andn bitwise exclusive OR (^) `?
To understand how the exclusive OR operator works try this code

 ``123456789`` ``````#include int main() { std::cout << "0 ^ 0 = " << ( 0 ^ 0 ) << std::endl; std::cout << "1 ^ 0 = " << ( 1 ^ 0 ) << std::endl; std::cout << "0 ^ 1 = " << ( 0 ^ 1 ) << std::endl; std::cout << "1 ^ 1 = " << ( 1 ^ 1 ) << std::endl; }``````

Value of sub-expression (6*6+6) is equal to 42.
Value of sub-expression (2-9%4) is equal to 1

So in the last bits of the values we will have 0 (for 42) and 1 ( for 1 )

0 ^ 1 gives 1

All other bits of result value will not be changed. So we will get 43.

As for the second question then try the code I suggested with the operator | and look the result.
Last edited on
Understanding bitwise operations on decimal values such as 42 is hard to visualise. It helps to first convert it to binary, for example using the online tool here: http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

As for the operations, there's a tutorial here: http://www.cprogramming.com/tutorial/bitwise_operators.html
The bitwise or will return the following (just a normal or)
 ``123`` `````` 0011 or 1001 1011``````

The bitwise xor will return:
 ``123`` `````` 0011 xor 1001 1010``````

Effectively when you xor two bits, the output is 1 if they are different and 0 if they are the same. When you use the ^ operator, you are xor'ing every bit in the numbers.

42 is 0010 1010

1 is 0000 0001

`42 ^ 1 = 43`
becomes
 ``123`` `````` 0010 1010 xor 0000 0001 0010 1011``````

0010 1011 is binary for 43

Note, the ^ operator is NOT the exponent operator.
Last edited on
The bitwise operators deal with bits, so it is better to visualize bits rather than integers.

For example, the number 5 is: 0101
and the number 6 is : 0110

The result of OR (|) has every bit that is 1 "turned on"

0101 | 0110 = 0111 -- 5 | 6 = 7

The result of XOR (^) has every bit that is 1 that is 0 in the other "turned on"

0101 ^ 0110 = 0011 -- 5 ^ 6 = 3
so, for dealing with `^` and `|`
we ust convert give values to base 2 and then make the operation right?
Topic archived. No new replies allowed.