Question on Signed integer

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

  1. M

    M Guest

    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.

    Please let me have your comments or please
    direct me to some websites for clarifications.

    Thx
    M, Jun 27, 2004
    #1
    1. Advertising

  2. "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
    511> (one link)
    Boudewijn Dijkstra, Jun 27, 2004
    #2
    1. Advertising

  3. M

    Roedy Green Guest

    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.

    see also http://mindprod.com/jgloss/binary.html
    and
    http://mindprod.com/jgloss/precedence.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 28, 2004
    #3
  4. 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.
    >
    > Please let me have your comments or please
    > 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
    #4
  5. M

    M Guest

    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
    > 511> (one link)
    >
    >
    M, Jun 28, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kwaj
    Replies:
    2
    Views:
    712
  2. Christopher Dyken

    32x32 and 64x64 signed integer multiplication

    Christopher Dyken, Feb 17, 2004, in forum: C Programming
    Replies:
    18
    Views:
    1,421
    glen herrmannsfeldt
    Feb 24, 2004
  3. Replies:
    14
    Views:
    2,065
    CBFalconer
    Jun 18, 2005
  4. kyrpa83
    Replies:
    1
    Views:
    616
    kyrpa83
    Oct 17, 2007
  5. Rob1bureau
    Replies:
    1
    Views:
    798
    joris
    Feb 27, 2010
Loading...

Share This Page