posted 6 years ago

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?

SCJA 6 (Studying for SCJP 6)

Ashish Schottky

Ranch Hand

Posts: 93

posted 6 years ago

@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.

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.

Ashish Schottky

Ranch Hand

Posts: 93

posted 6 years ago

@Jared:

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.

To learn more about this operator, I will suggest you to write small program which will take in two integers, and print their exor.

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

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.

To learn more about this operator, I will suggest you to write small program which will take in two integers, and print their exor.

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