Thread: Two's Complement

  1. #1
    Unregistered
    Guest

    Two's Complement

    Hey Guys,

    I know this isn't a direct question about C, but it does relate.

    When you want to get a two's complement of a number, you first get the one's compelement (or logicaly NOT it) and then add 1.

    example:

    0000 0011 = 3

    1111 1100 = not
    +1
    ---------------
    1111 1101 = -3


    So is the 2's complement just a unique number? Do I have to get over the fact that 1111 1101 isnt |3| in binary, but just unique?

    1000 0000 = 128

    0111 1111
    +1
    --------------
    1000 0000 = -128
    is -128 just an exception to the rule? i mean the sign bit has a double meaning?

  2. #2
    Unregistered
    Guest
    Not exactly sure what you're asking. For subtraction, you add the 2's complement. You get funny results when your numbers get large.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Let's speak about bytes.
    A byte is 8 bits long so from
    00000000 eg 0
    to 11111111.
    It's a char in C.

    Whether or not a char is signed or unsigned (by default) is dependant on the processor.

    If chars are unsigned 11111111 is 255 and a byte goes from
    0 to 255. There's no sign bit.

    If chars are signed the rightest bit is the sign bit so 11111111 is
    -156 and a byte goes from -156 to 155. The sign bit has a single meaning "-".

    It's the same with words or longs.

    Of course you can choose what you want with
    signed char a;
    unsigned char b;

  4. #4
    Unregistered
    Guest
    As for as I know, when I say you get bad results with large numbers, you never want to use numbers that are large enough to fill the largest bit in the registers. Also, I've never seen this stated, but, between negative and positive numbers, you have a zero, and that is why you add the 1 to make a 2's comp.

  5. #5
    Unregistered
    Guest
    a signed byte has a range of -128...127. not -156..155, gertfaller

    2^Number of Bits - 1 = Largest Value

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Of course ...

  7. #7
    Sayeh
    Guest
    >> If chars are signed the rightest bit is the sign bit

    Actually, it's generally the leftmost bit unless you're using an older compiler who doesn't compensate for the 'endian' difference between motorola and intel CPUs.

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > the 'endian' difference between motorola and intel CPUs.

    Motorola's big endian and Intel's little, right? (doing a little Micros review in my head)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For signed numbers, the most significant bit (msb) is the sign bit:
    If msb = 0, number is positive
    If msb = 1, number is negative

    1000 0000 = -128

    Positive 128 can't be stored in 8 bits (unless you aren't representing negative numbers).
    Last edited by swoopy; 02-25-2002 at 02:31 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Thank you, Govtcheez!
    I just wondered what "little endian" means.

    klausi
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two's complement
    By alyeska in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 09:55 PM
  2. sscanf and 32 bit two's complement
    By The Urchin in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 02:17 AM
  3. two's complement question
    By NetWeirdo in forum C Programming
    Replies: 1
    Last Post: 12-10-2005, 02:36 PM
  4. Question regarding 8/16-bits 2's complement numbers
    By mobius in forum C Programming
    Replies: 1
    Last Post: 09-02-2001, 11:49 PM