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.
Related
This question already has answers here:
Java: What does ~ mean
(5 answers)
Closed 5 years ago.
int x=10;
System.out.println(~x);
//this will print -11
//how to do the calculation manually using complement arithmetic
This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this
Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;
int x = 10;
System.out.println(Integer.toBinaryString(x)); //00000000000000000000000000001010
System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
System.out.println(~x); //-11
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
I was solving a problem in leet code and noticed the following code was not allowed in Java,
char c = 's';
c = c^c;
While the following was
char c = 's';
c^=c;
Is there a particular reason? Thanks you.
This is also true for plus or minus. c^c is evaluated as an int so the right hand side is int and can not be assigned into a char.
In the ^= case, the right hand side is char, and can be applied onto a char.
This is not the most obvious behavior.
This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 7 years ago.
Is += the same as =+?
I can't find any reason for why the plus sign is reversible.
For what reasons would I need to use one of the other? Where can i find docs on this i tried searching but didnt see the use of both.
It's not the same.
x+=5 is equivalent to x=x+5.
x=+5 (or x=(+5)) is equivalent to x=5.
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.
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?