Thread: 2's complement in C

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Unhappy 2's complement in C

    Hai,

    1) In one book, I have read that in C Programming, to store negative numbers , 2's complements are used. Why is it like that ? Is there any advantage to use 2's complement?(Positive numbers will be stored in ordinary binary format, Am I correct?)

    2) How can I represent (-128) in 2's complement form?

    Advance thanks for your responses.

    -Kokila

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: 2's complement in C

    Originally posted by Kokila
    Hai,


    2) How can I represent (-128) in 2's complement form?
    Code:
    int foo;
    
    foo = -128;
    hello, internet!

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: 2's complement in C

    Originally posted by Kokila
    Hai,

    1) In one book, I have read that in C Programming, to store negative numbers , 2's complements are used. Why is it like that ? Is there any advantage to use 2's complement?(Positive numbers will be stored in ordinary binary format, Am I correct?)

    2) How can I represent (-128) in 2's complement form?

    Advance thanks for your responses.

    -Kokila
    For signed values, the most significant bit is set to 1 for negative values....on positives, it is set to 0

    -128 = 1111 1111 1000 0000

    +128 = 0000 0000 1000 0000

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Whether C uses 2's compliment is specific to the machine, not to C. C covers up details like this so that if you write conforming programs it won't matter if the machine is 2's compliment or not.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two's complement question
    By NetWeirdo in forum C Programming
    Replies: 1
    Last Post: 12-10-2005, 02:36 PM
  2. 1's and 2's complement
    By Jaguar in forum C Programming
    Replies: 3
    Last Post: 10-14-2003, 09:12 AM
  3. Two's Complement
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 02-26-2002, 05:52 AM
  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