This week's book giveaway is in the Android forum.We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# reverse operator

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What will be the output when you compile and execute the following program.

public class Base{
private void test() {
System.out.println(~6);
}

static public void main(String[] a) {
new Base().test();
}
}

a) 0
b) false
c) -6
d) Compilation Error.Incompatible type for ~. Can't convert int to boolean.
e) ~6
f) -7
I would like to know why the answer is (f) and how do you approach this problem
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
I think this question has already been discussed. -7 is bitwise complement of 6. Note that the operator ~ is supposed to reverse the bits of the operand.

Saravanan
Christian Strohmaier
Greenhorn

Joined: Feb 10, 2000
Posts: 8
A small rule of thumb for getting the bit representation of negative numbers (useful for >> >>> << questions):
1. consider the absolute value of the number
2. decrement this number
3. get the bit representation
4. invert every bit
Example:
bit representation of byte -14?
1. 14
2. 13
3. 00001101
4. 11110010
Tony Alicea
Sheriff

Joined: Jan 30, 2000
Posts: 3222

5
You may have to learn binary for this. For economy of display space, I'll use the integral <CODE>byte</CODE> which occupies, as you know, eight bits:
the number 6 is
00000110
in binary.
"not" 6 (that is, ~6)
is all bits inverted:
11111001
which is a negative number because the most significant bit is '1'.
That happens to be -7.
to find the negative of a positive number, flip all bits and add one:
7 = 00000111
flipping all bits gives
11111000
11111001
which is -7.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Gilbert Baron
Greenhorn

Joined: Feb 12, 2000
Posts: 23
I at first did it this way too but there is a much easier way. You are using 2s complement notation here. Simply invert the bits and ad one. It is easier and one less step. It works in both directions. That is you can construct the negative number starting with the positive or determine what the negative number is starting with the negative
11111001
invert
00000110 = 6
= 7.
In reverse to constuct the negative 7
00000111
invert
11111000
11111001

Of course for positive numbers there is no problem (and no adding either)
00000110 is 6, period.
Originally posted by Christian Strohmaier:
A small rule of thumb for getting the bit representation of negative numbers (useful for >> >>> << questions):
1. consider the absolute value of the number
2. decrement this number
3. get the bit representation
4. invert every bit
Example:
bit representation of byte -14?
1. 14
2. 13
3. 00001101
4. 11110010

"Hierro candente, batir de repente"
M Bala
Greenhorn

Joined: Oct 31, 2003
Posts: 10
As per JLS:
For all cases, ~x equals (-x)-1
[ November 20, 2003: Message edited by: M Bala ]
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
This is the simplest technique (in my opinion):
-x = ~(x - 1) (This is equivalent to what Bala mentioned above)
Examples:
-1 = ~(1 - 1) = ~0
-2 = ~(2 - 1) = ~1
-3 = ~(3 - 1) = ~2
so on and so forth ...
Cheers
Harwinder
Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
I learned that way and I think it's very easy:
~x = (x + 1) * (-1)
Add one then change the signal.
Examples:
~0 = (0 + 1) * (-1) = -1
~6 = (6 + 1) * (-1) = -7
~31 = (31 + 1) * (-1) = -32
~(-1) = (-1 + 1) * (-1) = 0
~(-2) = (-2 + 1) * (-1) = 1
~(-9) = (-9 + 1) * (-1) = 8
~(-32) = (-32 + 1) * (-1) = 31
(this is the same as Bala and Harwinder said...)
[ November 20, 2003: Message edited by: Ana Abrantes ]

Ana<p>SCJP 1.4
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Originally posted by Ana Abrantes:
I learned that way and I think it's very easy:
~x = (x + 1) * (-1)

It's the same as Bala mentioned above, just a bit more convoluted
If you expand RHS:
~x = (x * -1) + (1 * -1)
=> ~x = -x - 1 (Same as Bala mentioned above)
Enjoy !
Harwinder
Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
Originally posted by Harwinder Bhatia:

It's the same as Bala mentioned above, just a bit more convoluted

I tried to explain mathematically and it turned really awful, I agree with you. :roll:
What I meant is exactly you're version but I haven't realized it until I posted mine.
Let me try to correct myself.
That's my rule to solve this problem:
"get the number, add one to it and then change the sign."
~x => -(x + 1)

I agree. Here's the link: http://aspose.com/file-tools

subject: reverse operator