Can anyone explain how do we get this output ? (bitwise not) [duplicate] - java

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

Related

puzzle 65-java puzzlers. int overflow can occur in calculations involving subtractions of negative numbers, int.Max_Value, etc How do you avoid it? [duplicate]

This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001

What does << mean? [duplicate]

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.

java double number division giving strange output [duplicate]

This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

calculating the power in Java [duplicate]

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.

why does 2^0 return 2 in java [duplicate]

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?

Categories

Resources