Velocity Reviews > Java > Question on Signed integer

# Question on Signed integer

M
Guest
Posts: n/a

 06-27-2004
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

Boudewijn Dijkstra
Guest
Posts: n/a

 06-27-2004
"M" <(E-Mail Removed)> schreef in bericht
news:cbmteq\$rep\$(E-Mail Removed)...
> 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/vmspe...ts.doc.html#19

Roedy Green
Guest
Posts: n/a

 06-28-2004
On Mon, 28 Jun 2004 00:20:20 +0800, "M" <(E-Mail Removed)> 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.

John B. Matthews
Guest
Posts: n/a

 06-28-2004
In article <cbmteq\$rep\$(E-Mail Removed)>,
"M" <(E-Mail Removed)> 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/...am/2s_comp.htm

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

M
Guest
Posts: n/a

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

"Boudewijn Dijkstra" <(E-Mail Removed)> wrote in message
news:40df0fbe\$0\$62363\$(E-Mail Removed) i.nl...
> "M" <(E-Mail Removed)> schreef in bericht
> news:cbmteq\$rep\$(E-Mail Removed)...
> > 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/vmspe...ts.doc.html#19
>
>