This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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 Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# ~ operator

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider:

char v1 = '(';
int i = ~v1;
System.out.println(i);

Why does the above print a negative value (-41) when the answer we get is of the form 0xffffffd6 (the actual characters may be different), after changing the 1s to 0s and vice-versa.
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
What does operator~ take as its operand?
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
0xffd6 = 1111 1111 1101 0110
-----------------------------
The most left bit is 1, means that this is negative integer.

0000 0000 0010 1001 = - (32 + 8 + 1) = -41
Ranch Hand

Joined: Oct 29, 2004
Posts: 52
Can someone please explain the basic steps involved in the ~ operation.
For example,
int x = 9;
x = ~ x;

Step 1: Convert 9 to binary: 1001
Step 2: Since the signed bit is one, the answer should be a negative integer
Step 3: Now convert 0's to 1 and one's to 0. That is, 0110
This is wrong. The answer is -10.
What am I doing wrong here? Please explain.
Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

Now we have a bit pattern that we must convert to decimal. The high bit is 1 so it's negative. So let's take the two's complement and then afterwards call the number negative again.

So our answer is the negative of this: -10.

Getting someone to think and try something out is much more useful than just telling them the answer.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Sumitabh,

Step 1> Represent 9 in 32 bit integer form.
0000 0000 0000 0000 0000 0000 0000 1001 = 9

Step 2> One's complement of 9
1111 1111 1111 1111 1111 1111 1111 0110

Step 3> To get the final answer do 2's complement on the result obtained in step 2 and add the negative sign.

Hope this helps you.

Always represent number in full 32 bit integer form This is very important when you are trying to play around with bit operators.

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In fact a bit of algebra "proves" it: Start with a positive integer x.

-x == ~x + 1 // the negative is found by flipping the bits (~x) and adding 1

Subtracting 1 from both sides:

-x - 1 == ~x

That is:
~x == -(x + 1)

So for example: ~9 == -(9 + 1) == -10.

Exercise: does it work if x < 0? for Integer.MAX_VALUE? for Integer.MIN_VALUE?
[ December 06, 2004: Message edited by: Barry Gaunt ]
Ranch Hand

Joined: Oct 29, 2004
Posts: 52
Thanks Barry for the short cut method. ~x = -(x+1).
It works for x <0, x= Integer.MAX_VALUE, Integer.MIN_VALUE.

So, this is all I need to know for ~x. Or do I definitely need to do it the long way - converting to one's compliment and then adding 1 to the result? Let me know so that I can move on.
Thanks once again.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Sumitabh,
For Integer.MIN_VALUE , the formula doesn't hold good. However, for ~Integer.MAX_VALUE will give you Integer.MIN_VALUE as per the formula.

In short,

~Integer.MIN_VALUE = Integer.MAX_VALUE
~Integer.MAX_VALUE = Integer.MIN_VALUE
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Jay Pawar:
Sumitabh,
For Integer.MIN_VALUE , the formula doesn't hold good. However, for ~Integer.MAX_VALUE will give you Integer.MIN_VALUE as per the formula.

In short,

~Integer.MIN_VALUE = Integer.MAX_VALUE
~Integer.MAX_VALUE = Integer.MIN_VALUE

But interestingly:

prints: true true

You have to think in a 32bit universe you see. And -2147483649 is not in that universe.

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

subject: ~ operator