# Question on Signed integer

Discussion in 'Java' started by M, Jun 27, 2004.

1. ### MGuest

Hi,

I was told that an Java int is a signed 32 bits integer and
ranged from -2^31 to 2^31 -1 (^: to the power of).

Then, I would like to know if -31 should be represented by:
(1) 10000000 00000000 00000000 00011111, or
(2) 11111111 11111111 11111111 11111111

(1) or (2)?

I think it's (2) because -31 >>> 5 yields 3
but I found no doc from java.sun.com relating to this.

direct me to some websites for clarifications.

Thx

M, Jun 27, 2004

2. ### Boudewijn DijkstraGuest

"M" <> schreef in bericht
news:cbmteq\$rep\$...
> Hi,
>
> I was told that an Java int is a signed 32 bits integer and
> ranged from -2^31 to 2^31 -1 (^: to the power of).

Correct.

> Then, I would like to know if -31 should be represented by:
> (1) 10000000 00000000 00000000 00011111, or
> (2) 11111111 11111111 11111111 11111111

Both wrong. It is:
(3) 11111111 11111111 11111111 11100001

(1) in decimal would be -2147483617 (0x80000000 + 31)
(2) in decimal would be -1

Because the integer is so-called twos-complement.
This means that the equation
-x == ~x + 1
is true for all 2's-complement binary integers with a fixed number of bits.
(And where the '~'-operator represents bitwise NOT.)

> I think it's (2) because -31 >>> 5 yields 3

I don't understand your reasoning. Can you elaborate, assuming that the
reasoning is still valid after my answer?

> but I found no doc from java.sun.com relating to this.

The fact that integers are 2's-complement is explained here:
<http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19

Boudewijn Dijkstra, Jun 27, 2004

3. ### Roedy GreenGuest

On Mon, 28 Jun 2004 00:20:20 +0800, "M" <> wrote
or quoted :

>Then, I would like to know if -31 should be represented by:
>(1) 10000000 00000000 00000000 00011111, or
>(2) 11111111 11111111 11111111 11111111
>
>(1) or (2)?

You need to do some experiments. See
http://mindprod.com/converter.html to learn to dump stuff in binary.

and
http://mindprod.com/jgloss/precedence.html

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green, Jun 28, 2004
4. ### John B. MatthewsGuest

In article <cbmteq\$rep\$>,
"M" <> wrote:

> Hi,
>
> I was told that an Java int is a signed 32 bits integer and
> ranged from -2^31 to 2^31 -1 (^: to the power of).
>
> Then, I would like to know if -31 should be represented by:
> (1) 10000000 00000000 00000000 00011111, or
> (2) 11111111 11111111 11111111 11111111
>
> (1) or (2)?
>
> I think it's (2) because -31 >>> 5 yields 3
> but I found no doc from java.sun.com relating to this.
>
> direct me to some websites for clarifications.
>
> Thx

11111111 11111111 11111111 11111111 = -1.
11111111 11111111 11111111 11100001 = -31.

http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm

John
----
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/

John B. Matthews, Jun 28, 2004
5. ### MGuest

Thanks. After I understand the 2-comp's stuff, it's solved.
Cheers.
M.

"Boudewijn Dijkstra" <> wrote in message
news:40df0fbe\$0\$62363\$...
> "M" <> schreef in bericht
> news:cbmteq\$rep\$...
> > Hi,
> >
> > I was told that an Java int is a signed 32 bits integer and
> > ranged from -2^31 to 2^31 -1 (^: to the power of).

>
> Correct.
>
> > Then, I would like to know if -31 should be represented by:
> > (1) 10000000 00000000 00000000 00011111, or
> > (2) 11111111 11111111 11111111 11111111

>
> Both wrong. It is:
> (3) 11111111 11111111 11111111 11100001
>
> (1) in decimal would be -2147483617 (0x80000000 + 31)
> (2) in decimal would be -1
>
> Because the integer is so-called twos-complement.
> This means that the equation
> -x == ~x + 1
> is true for all 2's-complement binary integers with a fixed number of

bits.
> (And where the '~'-operator represents bitwise NOT.)
>
> > I think it's (2) because -31 >>> 5 yields 3

>
> I don't understand your reasoning. Can you elaborate, assuming that the
> reasoning is still valid after my answer?
>
> > but I found no doc from java.sun.com relating to this.

>
> The fact that integers are 2's-complement is explained here:
>

<http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19