2's complement

Discussion in 'Java' started by eeh, May 30, 2005.

  1. eeh

    eeh Guest

    Hi,

    Could anyone give me sample code to calculate 2's complement of an
    integer which is n (n<16) bits length?

    Thanks!
    eeh, May 30, 2005
    #1
    1. Advertising

  2. On 29 May 2005 20:57:02 -0700, eeh wrote:

    > Could anyone give me sample code to calculate 2's complement of an
    > integer which is n (n<16) bits length?


    Can you..
    a) Read your course note?
    b) Consult your lecturer or tutor?
    c) Google it?
    d) Offer cash?

    <http://www.physci.org/codes/javafaq.jsp#homework>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, May 30, 2005
    #2
    1. Advertising

  3. eeh

    Lucy Guest

    "eeh" <> wrote in message
    news:...
    > Hi,
    >
    > Could anyone give me sample code to calculate 2's complement of an
    > integer which is n (n<16) bits length?
    >
    > Thanks!


    int a = 5;
    int b = ~a;
    Lucy, May 30, 2005
    #3
  4. Lucy wrote:

    > "eeh" <> wrote in message
    > news:...
    >
    >>> Hi,
    >>>
    >>> Could anyone give me sample code to calculate 2's complement of an
    >>> integer which is n (n<16) bits length?
    >>>
    >>> Thanks!

    >
    >
    > int a = 5;
    > int b = ~a;
    >
    >



    That will give the 1's complement, the result of flipping
    each bit. You can get the 2's complement in several ways but
    the simplest is:

    int b = -a;

    Patricia
    Patricia Shanahan, May 30, 2005
    #4
  5. eeh

    Guest

    "Lucy" <> wrote in message
    news:...
    >
    > "eeh" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> Could anyone give me sample code to calculate 2's complement of an
    >> integer which is n (n<16) bits length?
    >>
    >> Thanks!

    >
    > int a = 5;
    > int b = ~a;


    I wonder whether the op wants to find
    the two's complement form of an integer?
    , May 31, 2005
    #5
  6. In article <>,
    "Lucy" <> wrote:

    > "eeh" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > Could anyone give me sample code to calculate 2's complement of an
    > > integer which is n (n<16) bits length?
    > >
    > > Thanks!

    >
    > int a = 5;
    > int b = ~a;


    That's one's complement. Two's is one value higher.
    Kevin McMurtrie, May 31, 2005
    #6
    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. Troels Smit

    Conversion 1QN -> 2'Complement

    Troels Smit, Jun 30, 2003, in forum: VHDL
    Replies:
    1
    Views:
    4,143
    Troels Smit
    Jul 1, 2003
  2. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,569
    Glen Herrmannsfeldt
    Oct 8, 2003
  3. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,166
    Bo Persson
    Aug 2, 2006
  4. sarathy
    Replies:
    22
    Views:
    2,311
    Bo Persson
    Aug 2, 2006
  5. Roberto Waltman

    2's complement vs. 1's complement vs. ...

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,333
    Michael Press
    Jun 14, 2011
Loading...

Share This Page