This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 8 years ago.
What does the ^ (caret) mean in Java syntax? Why does 6^3 return 5?
It's the bitwise XOR operator. XOR is exclusive or.
6 in binary (lets assume 4 bits) is 0110, 3 in binary is 0011.
So, we get:
0110
0011 XOR
----
0101
And 0101 is 5.
See Bitwise and Bit Shift Operators.
Related
This question already has answers here:
How do you specify a byte literal in Java?
(6 answers)
How to specify a constant is a byte or short?
(3 answers)
Java binary literals - Value -128 for byte
(1 answer)
Closed 4 months ago.
So to convert a positive binary number into a negative one I have to invert all of the bits of the number and add 1.
`byte x = 0b0111_1111;
System.out.println(x); // 127
x = 0b1111_1111_1111_1111_1111_1111_1000_0001;
System.out.println(x); // -127
I don't understand why I have to expand my number to 32 bit form by adding zeros on the left side before reversing the bits, why exactly to 32 bit form?
This question already has answers here:
How does bitshifting work in Java?
(10 answers)
Closed 7 years ago.
What are these double lesser than symbols. I've had to use them but I have no idea what they are. It seems to be comparing just like the == but I've never heard of it before. Thanks for any help.
<< (left shift)
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
Example: Lets say A = 60. So,
A = ...000111100
A << 2 will give
A = ...011110000
which in decimal is 240.
Look at this example to learn more.
This question already has answers here:
Bitwise operator for simply flipping all bits in an integer?
(16 answers)
Closed 8 years ago.
I would like to flip all the bits in a byte. How would I do that?
Ex:
input: 10101000
output: 01010111
Use the ~ bitwise complement operator.
byte flipped = (byte) ~original;
This question already has answers here:
Clarification about "int" number that begins with 0
(4 answers)
Closed 9 years ago.
I have an array storing a series of numbers as follows:
static final int CodeArray[] ={
11,
011,
0011,
1011,
00011,
10011,
01011,
000011,
100011,
010011,
...
}
However when I access the values(using a for loop), it returns the following:
11
9
9
1011
9
10011
521
9
100011
4105
Why is there a difference in the values being printed from the ones stored?
Because putting a 0 before a number turns it into an octal representation, not binary.
So, for example, 011 is octal for decimal 9, which is what's printed.
See this SO question to see how to work with binary numbers in Java.
011 is treated as octal not decimal
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the ^ operator do in Java?
The power ^ in Java?
I am sorry if this is a duplicate, but i didn´t found anything in SO.
So can someone explaint me why
System.out.println((2^0));
this does return 2?
i was expecting a 1.
Because the ^ operator does not mean "raise 2 to the 0th power". It's a bitwise exclusive OR operator.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
In order to do that, your code should look like this:
double one = Math.pow(2.0, 0.0); // Silly, but you can do it.
Don't be surprised if the answer comes out to something that's not exactly 1.0. You'll need to know about how floating point numbers work.
The ^ sign means XOR and not pow. Try Math.Pow(2.0, 0.0) instead.
^ in Java is Bitwise exclusive-OR.
so 2(1 0) ^(XOR) 0(0 0) =1 0 ie 2 !!!
Got it?