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.

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

Joined: Oct 31, 2005
Posts: 389

posted

0

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.

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?

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 ]

SCJP 1.4 - 95%

Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389

posted

0

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.

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.