Thread: Negative numbers in C

  1. #16
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    32 bit computers have 32 bit registers. If you were to add two 32 bit integers together and the result takes more than 32 bits of space the remainder goes into another register. I don't remember which register that is however.

    EDIT:

    It's the FLAGS register.
    Last edited by ITAmember; 06-29-2009 at 11:42 AM.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    32 bit computers have 32 bit registers. If you were to add two 32 bit integers together and the result takes more than 32 bits of space the remainder goes into another register. I don't remember which register that is however.

    EDIT:

    It's the FLAGS register.
    Well, when you do an add there's only one bit that can carry, so that just sets the carry flag (it doesn't set the entire register). Multiplication takes two registers (the usual accumulator eax gets the least significant part, I think, and then one of the other registers gets the high part).

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by BlaX View Post
    I was wondering .. where do these conversions take place ? im sure they won't be at the memory level because that will cause overriding and overflows in memory ... is it in the registers ?
    Yep in the registers.
    Quote Originally Posted by BlaX View Post
    Anyway why should they be converted afterall?Whats the purpose behind it.
    Because two byte-sized values can add up to give a number that's greater than a byte in length. So the micro fills up the register with sign bits(or not) if unsigned hasn't been specified when declaring the type of the variable.

    Edit: forgot to say that this happens for chars only because of default promotion rules built into the compiler.
    Last edited by itCbitC; 06-29-2009 at 02:58 PM.

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    one of the other registers gets the high part).
    EDX I believe.

    I was wondering .. where do these conversions take place ? im sure they won't be at the memory level because that will cause overriding and overflows in memory ... is it in the registers ?
    When you assign a char to a 16-bits short for example, the compiler will generate instructions to do sign extension or zero extension (depending on whether char is by default signed or not). For example, the MOVSX (move with sign extension) and MOVZX (move with zero extension) instructions. And of course, these instructions take clock cycles.

    C is a high level language (compared to assembly), and there are hidden costs everywhere.

    Anyway why should they be converted afterall?Whats the purpose behind it.
    They are converted when you tell the compiler to (when you assign a char to an int for example).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM