posted 3 years ago

Java uses two complement for the byte, short, int, and long representations...

http://en.wikipedia.org/wiki/Two's_complement

1111 1101 = -128 + 64 + 32 + 16 + 8 + 4 + 1 = -3

Since twos complement has the property that ... -X = ~X + 1

X = 1111 1101

-X = ~X + 1 = ~(1111 1101) + 1 = (0000 0010) + 1 = 2 + 1 = 3

X = -3

This second way may look harder, but is probably easier based on the value being converted -- as you can probably do it without a pen and paper.

Henry

Abigail Decan wrote:i need help understanding why

1111 1101 = -3

Java uses two complement for the byte, short, int, and long representations...

http://en.wikipedia.org/wiki/Two's_complement

**One way to do this:**

1111 1101 = -128 + 64 + 32 + 16 + 8 + 4 + 1 = -3

**Another way to do this:**

Since twos complement has the property that ... -X = ~X + 1

X = 1111 1101

-X = ~X + 1 = ~(1111 1101) + 1 = (0000 0010) + 1 = 2 + 1 = 3

X = -3

This second way may look harder, but is probably easier based on the value being converted -- as you can probably do it without a pen and paper.

Henry

Abigail Decan

Ranch Hand

Posts: 65

posted 3 years ago

You are going to have to provide more details -- as the wiki link that I mentioned certainly doesn't say that.

Henry

Abigail Decan wrote:wiki says the in two's complement, a negative number is the complement of its absolute value.

since 3 = 0000 0011, shouldn't

-3 = 1111 1100?

what's the extra 1 at the end doing there?

You are going to have to provide more details -- as the wiki link that I mentioned certainly doesn't say that.

Henry

posted 3 years ago

The article/wikipedia page Henry points to says:

This seems like a circular reference, but it's not...

"Two's-complement representation" is a way to store negative numbers, used by many computers. To do this, you take the absolute value of a number, then two's complement it.

In two's-complement representation, negative numbers are represented by thetwo'scomplement of their absolute value;[

This seems like a circular reference, but it's not...

"Two's-complement representation" is a way to store negative numbers, used by many computers. To do this, you take the absolute value of a number, then two's complement it.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie

Marshal

Posts: 56570

172

posted 3 years ago
1: You cannot talk about a two's complement number. What you showed is an 8‑bit two's complement number. Saying complement without saying how many digits is meaningless. 2: You must have a convention about how many values represent negative numbers: virtually always exactly 50%. A two's complement of −i in 8 bits is the value obtained by subtracting i from 2⁸ which is 1_0000_0000

1_0000_0000

1111_1101

If you convert 0000_0011 to 1111_1100, that is the one's complement. You can work out that one's complement + 1 = two's complement.

That is incorrect. There is a lot more to know about complementary numbers.Abigail Decan wrote:wiki says the in two's complement, a negative number is the complement of its absolute value. . . .

1_0000_0000

__0000_0011__−

1111_1101

If you convert 0000_0011 to 1111_1100, that is the one's complement. You can work out that one's complement + 1 = two's complement.

Abigail Decan

Ranch Hand

Posts: 65

It is sorta covered in the JavaRanch Style Guide. |