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
Related
This question already has answers here:
How can a Java variable be different from itself?
(11 answers)
Closed 5 years ago.
Recently I had an interview with a Software company where the following question was asked in the technical aptitude round:
Declare i in such a way that the condition is always true :
while(i != i) {
}
Is it technically possible in java to assign something of this sort??
NaN is not equal to itself, so
double i = Double.NaN;
But I don't think this is a good interview question.
Quote from the Java Language Specification:
NaN is unordered, so:
The numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1).
The equality operator == returns false if either operand is NaN. In particular, (x<y) == !(x>=y) will be false if x or
y is NaN.
The inequality operator != returns true if either operand is NaN (§15.21.1). In particular, x!=x is true if and only if x
is NaN.
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.
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 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.
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.