programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# negative numbers in binary

Abigail Decan
Ranch Hand
Posts: 65
i need help understanding why

1111 1101 = -3

Henry Wong
author
Sheriff
Posts: 23295
125
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
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?

Henry Wong
author
Sheriff
Posts: 23295
125
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

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
In two's-complement representation, negative numbers are represented by the two's complement 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.

Campbell Ritchie
Marshal
Posts: 56570
172
Abigail Decan wrote:wiki says the in two's complement, a negative number is the complement of its absolute value. . . .
That is incorrect. There is a lot more to know about complementary numbers.
• 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
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
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