This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
^ operator in java
I was assuming that c ^ d is a calculation like 'the power of', so c = 5, d = 2, result is 25. I think I'm wrong, though.
Can you explain what (c ^ d) does in java, for example in
result = result + (char)(c ^ d)
^ is the bitwise xor meaning that 0b0101^0b0010 (5^2) is 0b0111 and 0b0101^0b0111 is 0b0010
look at the truth table of xor (the result is 1 if the input are different
a b | a^b
---------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
the bitwise operators take each bit of the terms and apply the operator to each bit
The ^ operator performs a Bitwise Exclusive OR.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
For raising a number to a power, you would use the Math.pow function.
That one is the bitwise XOR operator.
^ is a bitwise XOR.
Related
This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 6 years ago.
I cannot understand to why this syntax does not generate any kind of compile time or run time errors ?
int i=2;
switch(i ^ 3){ ---- > this part
case 8: System.out.print("Eight"); break;
default: System.out.print("Default");
}
It prints Default, so what does this ( i ^ 3 ) do in the switch condition ?
i ^ 3 is i XOR 3 (2 XOR 3), which is 1 (10 XOR 11 is 1). It's not a power operator, so it doesn't return 8. Therefore the default section of the switch statement is reached.
i is equal to 2.
2^3 = 1. (XOR operation).
10 //2
11 // 3 (XOR)
--
01 //1
So, it sets the value of 1 for the switch condition.
^ is Bitwise XOR where i ^ 3 generate integer result.
I was looking source code of a java project, herein i found an operator |= for boolean variables.
Can anybody tell me what exactly is this? and best way to use this.
Thanks for reply, now improving my question: what |= shorthand operator actually does. as per my test it shows:
false | true = true
false | false = false
true | false = true
true | true = true
But i still not clear, how it determines the result. And any use case where i can use this.
Thanks
a |= b; means the same as a = a | b;, in the same way that a += b; means the same as a = a + b;.
You would use it whenever you have something of the form a = a | b; (which is rare) and want to shorten it.
As you have got your answer that it is a short and a compound assignment operator. So if you write
a1 |= a2;
or
a1 = a1 | a2;
the both mean the same thing. Its just the way to write the code.
Regarding the two W's which you have asked i.e, when to use and why to use? is completely dependent upon the programmer as some programmer finds the first one as more readable and some find the latter.
Here is a list of other such operators.
Operator Description Example
----------------------------------------------------------------------------------------
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
----------------------------------------------------------------------------------------
It's a short form for an assignment doing a logic OR with the left-hand operand.
a|=b ---> a=a|b
You can do the same thing with other operators: +, -, *, &, ^ , etc.
Update:
Adding something because it seems you need a short explanation of the logical or: This operator returns true only if at least one of its boolean operands is true. See this page on wiki.
This question already has answers here:
Change sign using bitwise operators
(3 answers)
Closed 7 years ago.
I need to negate a number using shift operator.
Example:
If number = 5 then negation of 5 should be -5
If number =-5 then negation of -5 should be 5.
Not with the shiftoperator but there is other bitwise operator using them you can do this
int i = 10;
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
i = (~i)+1;
System.out.println(i);
result
-10
10
-10
10
BTW it is example of Two's complement and used for binary signed number representations
This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 8 years ago.
Java programming.
int i = 0;
int j = 1;
str.charAt(i) ^ str2.charAt(j)
What does mean ^ operator in java? And what is this operator reverse operation?
example
w ^ . = 121
T ^ W = 35
The bitwise ^ operator performs a bitwise exclusive OR operation.
Applied, this does:
false ^ false = false
false ^ true = true
true ^ false = true
true ^ true = false
When it comes to integer varibles (including the type char) the numbers are converted to their binary representation and then the operator takes place. For example:
3 ^ 5 = 011 ^ 101 = 110 = 6
^ indicates Binary XOR Operator copies the bit if it is set in one operand but not both.
Truth Table For XOR
This question already has answers here:
What does this sign exactly mean? |=
(5 answers)
Closed 8 years ago.
this is a function for changing bit value of image. what does |= and ^= mean?
private int setBitValue(int n,int location,int bit) {
int toggle=(int)Math.pow(2,location),bv=getBitValue(n,location);
if(bv==bit)
return n;
if(bv==0 && bit==1)
n|=toggle; // what does it do?
else if(bv==1 && bit==0)
n^=toggle; // what does it do?
return n;
}
Its the same short form as in +=
n |= toogle
is the same as
n = n | toogle
the | is here the binary-or operator
and ^ is the binary xor-operator
They are short hand assignment operations.
n|=toggle; is equivalent to n=n|toggle;
and
n^=toggle; is equivalent to n=n^toggle;
And
| is bitwise OR
^ is bitwise XOR
They're the bitwise OR equals and the bitwise XOR equals operators. They are mainly used for dealing with bit flags. I highly recommend this article if you want to learn more about bitwise and bit-shifting operations.
These are shorthand bitwise operators. Like += using |= is the same as:
a = a | b;
Read the Oracle Documentation about Bitwise and Bit Shift Operators for further information.