C++ QUESTION
Posts
Pages:
1
I'm doing a C++ course at my school and I haz a question.
Here is the original question from the quiz:
Wouldn't it be 59049?
No I don't think it would... what does ^= do?
Thanks in advance.
Here is the original question from the quiz:
What would be the value of X1 when the following code segment completes execution?
int x1 = 9, x2 = 5;
x1 ^= x2;
No I don't think it would... what does ^= do?
Thanks in advance.
I think it's to do with bitwise operation which to be honest I don't really get.
My guess is
9 (decimal number) in binary is 1001.
5 (decimal number) in binary is 0101.
If the bits in each column are the same the resultant bit is 0. If they are different, the result is 1.
Only the last two columns (from the right) have two digits that are the same so the output (X1?) should be 1100.
My guess is
9 (decimal number) in binary is 1001.
5 (decimal number) in binary is 0101.
If the bits in each column are the same the resultant bit is 0. If they are different, the result is 1.
Only the last two columns (from the right) have two digits that are the same so the output (X1?) should be 1100.
I'm pretty sure C++ doesn't use "^" as "to the power of".
author=Yellow Magic link=topic=3520.msg70450#msg70450 date=1239569285
I'm pretty sure C++ doesn't use "^" as "to the power of".
Yeah it doesn't. This is the explanation of the "^+" operator:
"Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand."
So X1 = 1100 you say? I'll try it.
EDIT:
Actually, you know what. I think this is a trick question. The question is not outputting anything at all and nothing is being assigned. So X1 should remain the same value right?
The question asks for the value of X1, not the output (...which is the word I used, whoops).
And ^= is an assignment operator.
EDIT: Oooh, if we're using 8-bit bytes instead of nibbles (4-bits), maybe it's 00001100?
And ^= is an assignment operator.
EDIT: Oooh, if we're using 8-bit bytes instead of nibbles (4-bits), maybe it's 00001100?
author=Yellow Magic link=topic=3520.msg70453#msg70453 date=1239569975
The question asks for the value of X1, not the output (...which is the word I used, whoops).
And ^= is an assignment operator.
EDIT: Oooh, if we're using 8-bit bytes instead of nibbles (4-bits), maybe it's 00001100?
Oh man, so confusing :(
So ^= is an assignment operator you say?
My damn teacher never had us do anything with bitwise stuff. So i thought he was just trying to confuse us. Dammit i dunno. I have 3 attempts at this quiz, so I'm going to try the original value as the answer first and then try yours.
EDIT:
Here's an example I found online:
int a = 3, b = 2, result;
result= a ^= b;
cout << result << endl;
OUTPUT:
1
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.
^ 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















