New account registration is temporarily disabled.

KALDERIUS'S PROFILE

Search

Filter

C++ question

The answer ought to be 12.

^ is a basic operator in C++, much like +, -, *, /, etc. Like you said, ^ does a bitwise exclusive or (XOR).

So you're right about 1100. 9 is 1001 in binary, and 5 is 0101. XOR each bit and you get 1100. Then, take it back out of binary and you get 12.

You probably know that you can use commands like "X1 += 4" which takes the value of X1, adds 4 to it, and stores the result back into X1. ^= is probably the same way. I have absolutely no idea why you'd ever want to use ^=, but I guess it's there just in case.


More information, just in case you're interested:
& is the bitwise AND operator and | is the bitwise OR operator. When you're using an if statement and you want to check multiple conditions, you know to use && and ||. This is because && is the logical AND operator and || is the logical OR operator. If you just use one & or |, the program will execute the bitwise command.

In my experience, I most often use bitwise functions in regard to images, when each pixel is stored as an int and broken up into 4 parts: R, G, B, and alpha. With bitwise functions you can address only the portion of the integer which you wish to address, e.g. ANDing out the "Red" portion by using & 0000111111111111.
Pages: 1