switch( i ^ 3). what does this mean? [duplicate] - java

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.

Related

byte[] array in Java(size declaration) [duplicate]

This question already has answers here:
The power operator in Java?
(1 answer)
What does the ^ operator do in Java?
(19 answers)
Closed 1 year ago.
I have declared 3 different types of byte arrays(of different sizes). See the comment next to each as I am not able to understand how the length is computed by the compiler?
byte[] byteField0 = new byte[2^3];
System.out.println("bitField0 " + byteField0.length); // Gives 1 byte instead of 8?
byte[] byteField2 = new byte[2^5];
System.out.println("byteField2 " + byteField2.length); // Gives 7 bytes instead of 32?
byte[] byteField3 = new byte[8];
System.out.println("bitField3: " + byteField3.length); // Gives 8 bytes as expected
This has nothing to do with array size. Print those numbers individually, or more importantly, as binary.
^ is XOR bitwise operator, not a replacement for Math.pow
Or as mentioned in comments, powers of two can be accomplished with a different bitwise operator, the left shift <<, which would be computationally faster than Math.pow

How to negate a number using shift operator [duplicate]

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

How to use zero as prefix if value less than equals to 9 [duplicate]

This question already has answers here:
Left padding a String with Zeros [duplicate]
(20 answers)
Closed 8 years ago.
How to add zero as prefix if value less than equals to 9, i am using below way of achieving this:
int countZero = 0;
if(countVat <= 9)
{
countVat = countZero + countVat;
Log.d("countVat:", String.valueOf(countVat));
}
but this not works for me, still getting single digit if countVat value less than equals to 9.
Use String.format
String.format("%02d", num);

Java ^ operator [duplicate]

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

What does result = result + (char)(c ^ d) do? [duplicate]

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.

Categories