The ^ operation is exclusive or operation. It is defined in the following table.
value1 value2 result
0 0 0
0 1 1
1 0 1
1 1 0
So you see, it is value1 OR value2 but not value1 AND value 2
This is a bitwise operation.
The ^= operation works the same as the +=, -=, *=, etc operations.
a ^= b is equivalent to a = a ^ b
One cool thing about exclusive or is that it is reversible
if
x = a ^ b;
and
y = x ^ b;
then
y == a
Likewise, if
z = x ^ a
then
z == a
So you can use this to do things like store two pointers in a doubly-linked list in one location, for example, by exclusive-oring them together, at the cost, of course, of extra computation to reconstitute the originals. You can also use it to do a kind of cheap, low quality encryption, if you just want to obscure some data from prying eyes.
Can anybody explain me use of ^= assignment operator in C i need detailed explanation?
In C, ^ is the exclusive or (XOR) operator. Several operators including ^ and + can be placed immediately before the =. This means perform the operation and then the assignment. So for example
x ^= y ;
is shorthand for
x = x ^ y ;
Reply:Caret symbol is used for bitwise XOR.
so a^b will be bitwise XOR of the variables a and b.
When you use the grammar you mentioned the result of the bitwise XOR operation will be stored in the left hand side variable. For example:
a ^= b
is equal to a = a ^ b
Example:
Say a=1001 and b=0101 then:
a ^= b will have the same result as:
a = a ^ b = 1001 ^ 0101 = 1100
Reply:Hope this helps:
To declare a variable, use the following syntax:
data_type variable_name;
ex. To declare a variable called price that can be a real number:
float price;
To assign a value to variable, use the following syntax:
variable_name = value;
You can declare more than one variable on the same line and you can initialize a variable at the same time you declare it.
ex. int number, quantity;
float price = 5.25;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment