programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# ^= Usage

Jared Malcolm
Ranch Hand
Posts: 54
Can anyone explain this to me? Studying for the SCJP and ran across this and couldn't seem to find it used in code.... Anyone mind giving a description and a short code example?

Ashish Schottky
Ranch Hand
Posts: 93
@Jared: well it would be better if you gave us an entire question or atleast the reference of it.
From whatever the name of thread suggest, ^ is an ex-or operator.
and a^=b; is equivalent to a=a^b;
So by this way, I think this example might just help you to understand.

a^=b;
b^=a;
a^=b;

The above three lines would swap the numbers in a and b,without using 3rd element.

Jared Malcolm
Ranch Hand
Posts: 54
There was no question it was just listed in the book as an assignment operator and I had not seen it used.... So your saying....

Is the same as....

?

Ashish Schottky
Ranch Hand
Posts: 93
@Jared:
There was no question it was just listed in the book as an assignment operator and I had not seen it used.... So your saying....
view plaincopy to clipboardprint?

a ^= b;

Is the same as....

view plaincopy to clipboardprint?

a = !b;

?

No, this is not what I am saying.
Try wikipedia or google for the ex-or operator and its related operations.

! is boolean operator, which will see only true and false values.
^ is ex-or operator which will modify the values.

Consider the two bits a and b , and will list down the truth table for them.

a b a^b
0 0 0
0 1 1
1 0 1
1 1 0
When both bits are 1, the resultant of their exors will be 0.
say lets assume n=5and m=9;
converitng n in binary gives 101
converting m in binary gives 1001

0 1 0 1
^ 1 0 0 1
------------
1 1 0 0=12 in decimal
so 5^9=12.

so a sample program can be written as

Which is equivalent to

 It is sorta covered in the JavaRanch Style Guide.