On the Java website http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
The unary bitwise complement operator "~" inverts the bit pattern; This can be applied to any of the integral types, making each "0" a "1" and each "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit-bit is โ00000000โ, its pattern โ11111111โ will change.
Now, as Pascal MARTIN previously answered, in any case, the vlaue value is - (x) -1. For example. ~ 2 = -3, ~ -6 = 5, etc.
In addition, in java, all positive integers are stored as their binary representations, and negative integers are stored in 2 compliment values โโof a positive integer.
Now let's see how it works at the bit level in the case of ~ 2 = -3:
Initially, 2 is stored in binary representation:
0000 0000 0000 0010
Now ~ 2 will result in a value (inverts bits):
1111 1111 1111 1101
How in the world do I know this is -3? Well, this is -3 because it is derived from 2 complimentary representations of 3.
As we know, 2 (x) = 1 (x) + 1 ( https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/ )
Our goal is to find x :
1 (x) = 2 (x) - 1 (based on the previous expression)
As our answer is in 2 compliments,
1 (x) = 1111 1111 1111 1101 - 0000 0000 0000 0001
1 (x) = 1111 1111 1111 1100 (How to subtract - http://sandbox.mc.edu/~bennet/cs110/pm/sub.html )
Therefore, x = 1 compliment of the value (since the answer we received is 1 compliment of x).
x = 0000 0000 0000 0011 . So, we found that x is 3, and therefore our previous result of the ~ t25> -3 operator is written as 2 compliments of 3.
Dhwaneel Feb 18 '17 at 5:30 2017-02-18 05:30
source share