# ~ operator, explanation needed

Srikanth Raghavan

Ranch Hand

Posts: 389

Sireesha Mullapudi

Ranch Hand

Posts: 74

posted 9 years ago

- 0

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

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

Douglas Chorpita

Ranch Hand

Posts: 97

posted 9 years ago

- 0

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 ]

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 ]

SCJP 1.4 - 95%

Srikanth Raghavan

Ranch Hand

Posts: 389

posted 9 years ago

- 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.

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.

posted 9 years ago

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

- 0

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

posted 9 years ago

- 0

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.

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.

SCJP 1.4 - 95%

Bert Bates

author

Sheriff

Sheriff

Posts: 8898

5

It is sorta covered in the JavaRanch Style Guide. |