# how ~ operator works?

Ilakya Mukunth

Ranch Hand

Posts: 57

posted 3 years ago

Hi,

package Samples;

public class Byte1

{

public static void main(String[] a)

{

byte x = 3;

x = (byte)~x;

System.out.println(x);

}

}

I expected the output to be 12, but the output is -4. I know the operator ~ provides the 1's complement. But I could not interpret the output. Can anyone help?

package Samples;

public class Byte1

{

public static void main(String[] a)

{

byte x = 3;

x = (byte)~x;

System.out.println(x);

}

}

I expected the output to be 12, but the output is -4. I know the operator ~ provides the 1's complement. But I could not interpret the output. Can anyone help?

posted 3 years ago

Can you explain to us why you expected the output to be 12?

Henry

Ilakya Mukunth wrote:Hi,

package Samples;

public class Byte1

{

public static void main(String[] a)

{

byte x = 3;

x = (byte)~x;

System.out.println(x);

}

}

, but the output is -4. I know the operator ~ provides the 1's complement. But I could not interpret the output. Can anyone help?I expected the output to be 12

Can you explain to us why you expected the output to be 12?

Henry

Ilakya Mukunth

Ranch Hand

Posts: 57

posted 3 years ago

The bit pattern for 3 is 0011. The 1;s complement is 1100. The decimal value of this is 12.

posted 3 years ago

First, a byte is 8 bits long -- not four bits. Second, Java uses twos complement to store numbers -- which I have to ask, do you know how to interpret twos complement numbers?

Henry

Ilakya Mukunth wrote:The bit pattern for 3 is 0011. The 1;s complement is 1100. The decimal value of this is 12.

First, a byte is 8 bits long -- not four bits. Second, Java uses twos complement to store numbers -- which I have to ask, do you know how to interpret twos complement numbers?

Henry

Ilakya Mukunth

Ranch Hand

Posts: 57

posted 3 years ago

Well, you explained everything already. And you are correct in that regard, so I don't know what you are confused with.

First, as you already mentioned, the ~ operator negates all the bits (also known as 1's complement). So....

3 = 0000 0011

which means

~3 = 1111 1100

The resultant byte is a negative number as the sign bit is a one. There is actually a formula to see what this value is, but the easiest way is to take the twos complement of it to see what the positive version of this number is...

And, as you already mentioned, to do that, negate and add one. So...

X = 1111 1100

Negate X = 0000 0011

Negate X plus one = -X = 0000 0100 = 4

And since negative X equals four, X (the pre twos complement number) must have been -4.

Henry

- 1

Ilakya Mukunth wrote:I do not know java uses 2's complement to store the number. :-(

I know how to find 2's comp for a given number. (Add 1 to LSB to the 1's complement of the given number)

Can you please explain how the ~ operator works? I would be great help.

Well, you explained everything already. And you are correct in that regard, so I don't know what you are confused with.

First, as you already mentioned, the ~ operator negates all the bits (also known as 1's complement). So....

3 = 0000 0011

which means

~3 = 1111 1100

The resultant byte is a negative number as the sign bit is a one. There is actually a formula to see what this value is, but the easiest way is to take the twos complement of it to see what the positive version of this number is...

And, as you already mentioned, to do that, negate and add one. So...

X = 1111 1100

Negate X = 0000 0011

Negate X plus one = -X = 0000 0100 = 4

And since negative X equals four, X (the pre twos complement number) must have been -4.

Henry

Ilakya Mukunth

Ranch Hand

Posts: 57

posted 3 years ago

Thank you very much for your time and patience. Thanks a lot. Have a great day.

Henry Wong wrote:Henry Wong wrote:

There is actually a formula to see what this value is

BTW, just in case you are interested in the formula, the bit values are, from LSB to MSB of the byte, are ... 1, 2, 4, 8, 16, 32, 64, and -128. So...

1111 1100 = 4 + 8 + 16 + 32 + 64 - 128 = -4

Henry

Thank you very much for your time and patience. Thanks a lot. Have a great day.

posted 3 years ago

This would probably be a good time to make sure you are aware that ~ is not on the exam. Bit operations were removed from the SCJP.

[OCA 8 book] [OCP 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]

Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2

Ilakya Mukunth

Ranch Hand

Posts: 57

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |