Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reverse operator

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}

Select most appropriate answer.
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christian Strohmaier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
adding one gives
11111001
which is -7.
 
Gilbert Baron
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
add 1
= 7.
In reverse to constuct the negative 7
00000111
invert
11111000
add 1
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

 
M Bala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per JLS:
For all cases, ~x equals (-x)-1
[ November 20, 2003: Message edited by: M Bala ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic