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. Advertisements

  2. 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, May 30, 2005
    #2
    1. Advertisements

  3. eeh

    Lucy Guest

    int a = 5;
    int b = ~a;
     
    Lucy, May 30, 2005
    #3

  4. 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 Guest

    I wonder whether the op wants to find
    the two's complement form of an integer?
     
    Guest, May 31, 2005
    #5
  6. That's one's complement. Two's is one value higher.
     
    Kevin McMurtrie, May 31, 2005
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.