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

~ operator, explanation needed

 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ~ operator works the same way as ! operator for boolean.

In an example in KAM book,

Output:
-42

But how? They say that we have to invert the bits, but if I reverse the binary equivalent of 41 I don't get -42. Can someone explain me how this calculation is done.

Thank you!
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
if u apply ~operator to 41 u will get the binary string
11111111 11111111 11111111 11010110.which is nothing but the binary representation of -42.
-42=2's complement of 42.
just try it.
if iam wrong ,please correct me
thanks
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for replying!

I understood that if we apply ~ operator for 41 we get the following binary:

But why is '11111111 11111111 11111111 11010110' = -42. How did you calculate it? Basically I am struggling with binary representation of negative decimal numbers.

Thanks
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The representation of a negative whole number in computers is achieved through a "two's complement". Some very earlier computer languages used the keyword "TAD" to point out the "two's complement factor" when adding with negative numbers, for example.

Essentially negating a number is accomplished by complementing its bits and then incrementing the value.

How about some examples? Let's work with bytes, as these are easier to represent than 32-bit ints.


Example 1. negative of 0:
- this is 0: 0000 0000
- complement: 1111 1111
- increment: 0000 0000 (really 1 0000 0000, but the 1 gets lost)

Example 2. negative of 3:
- this is 3: 0000 0011
- complement: 1111 1100
- increment: 1111 1101 // this is -3, why?

let's increment it 3 times:
- increment: 1111 1110 // -2
- increment: 1111 1111 // -1
- increment: 0000 0000 // 0...overflowed bit gets lost.

Think about it! If the negation operator "-" is really just the same as "~" followed by "++", then (by "undoing" the increment) the complement operator "~" is really just the same as "-" followed by "--".

Therefore, "~x" equals "- x - 1" (or in Java syntax as "--(-x)" ). This can also be expressed (due to factoring out a -1) as "- (x + 1)" (or in Java as syntax "-(++x)" ). This is basic math.

Got it now?
[ July 20, 2006: Message edited by: Douglas Chorpita ]
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Douglas. I clearly understood what happens when a ~ operator is applied to a variable.

It's:
~x = - (x + 1)

But I still have this doubt. Let's say I am given a binary number 1111 1101 (which is -3). I understood that it's a byte and when we increment it 3 times, we would get zero and hence it's -3.

But, if someone gives me a binary number 1111 1101 and asks me to calculate the decimal equivalent (and says that it's a byte) how do I calculate? Currently I am doing this way,

the usual calculation comes to 253
and

253 - 256 = -3, and that's the answer.

But if this were to be an int, -3 will be 1111 1111 1111 1111 1111 1111 1111 1101 (if i am right). So, how should one calculate this easily without having to do the entire calculation and substracting it.

But your answer was clear and useful, thank you very much.
 
Aum Tao
Ranch Hand
Posts: 210
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For converting binary to decimal:

http://www.javaranch.com/campfire/StoryBits.jsp
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if this were to be an int, -3 will be 1111 1111 1111 1111 1111 1111 1111 1101 (if i am right). So, how should one calculate this easily without having to do the entire calculation and substracting it.


Two's complement, like negating a number, can also be applied to negative numbers to get positive numbers... so...

Once you noticed that the number is negative (sign bit), flip all the bits, and add one. You should end up with 3, which means the original number is negative 3.

Henry
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is right.

Take a difficult example:

1111 1111 1111 1111 0000 1100 1010 1001

The one in the first position means its a negative number. That's the sign bit. So complement and increment.

complement: 0000 0000 0000 0000 1111 0011 0101 0110
increment: 0000 0000 0000 0000 1111 0011 0101 0111

This is the positive version of your original negative number.

What to do now? I would convert to hexadecimal, then to decimal.

To convert to hexadecimal is easy. Just convert groups of four bits.

hex of increment: 0000F357 or F357.

Now use a calculator to convert to decimal or use multiplication.

F357 =
7 x 1 +
5 x 16 +
3 x 16^2 +
15 x 16^3

16^2 = 256
16^3 = 4096

This is a lot of multiplication, but there is no other way (except a calculator or computer).

The answer is 62295.

After you get the result of your complicated multiplication calculation, just add a minus sign to the front of it.

So, it's -62295.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and of course we should mention that this whole topic is appropriate only for those studying for the 1.4 exam.
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you one and all!

Got it now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic