#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    How long long int works?


    On a 32-bit machine maximum value an integer can store is 4,294,967,295 which is 2^32 -1. But long long int has a much larger maximum value!

    How does it store more than 2^32-1 when the limit is 32bits?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Number of bits has nothing to do with it. All that 32-bit says is that the CPU can add 32 bits at a time in a single operation. The CPU also has a bit field that knows if it needs to carry a digit over.

    Think about how you do addition. Normally humans can only add one digit at a time (e.g. 3 + 2, 7 + 6 etc.) That doesn't prevent you from adding larger numbers (e,g, 395 + 463, 10457 + 654 etc.) now, does it? The way you do it is you start from the right, add single columns of digits and apply the carry as needed e.g. if you were to add 395 + 463, you would start from the right columns and first add 5 + 3 and write 8, then add 9 + 6 and write 5 and carry 1 and then add 3 + 4 + 1 and write 8 and then write the result as 858. And you can do this with any large numbers, even though you only know how to add single digits at a time.

    The CPU does the same thing, except instead of adding a single digit at a time, it adds 32 digits at a time and applies the carry bit if needed. Of course, this means the compiler needs to generate multiple add instructions when your data type is long long int, but that is what the compiler is meant to do.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    Originally Posted by Avichal
    On a 32-bit machine maximum value an integer can store is 4,294,967,295 which is 2^32 -1. But long long int has a much larger maximum value!

    How does it store more than 2^32-1 when the limit is 32bits?
    In your system, what's the definition of long long int? If you don't know, then go to your compiler's INCLUDE directory and read limits.h. As for the standard, my copy is at work and I won't be able to read it again until Monday.

    On my system (Win7 64-bit) I have MinGW gcc 2.95.3-6 in which a long long int is 64 bits long. From MinGW gcc's limits.h:
    Code:
    #define LONG_LONG_MAX	9223372036854775807LL
    #define LONG_LONG_MIN	(-LONG_LONG_MAX-1)
    
    #define ULONG_LONG_MAX	(2ULL * LONG_LONG_MAX + 1)
    which translates to 9,223,372,036,854,775,807 signed and about twice that unsigned (calc fails me here).

    IOW, there is no 32-bit limit. On 16-bit systems, a long int was still 32 bits long. No 16-bit limit there and then. No 32-bit limit here and now.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Avichal
    How does it store more than 2^32-1 when the limit is 32bits?
    The 32-bit "limit" isn't a fundamental limit. You should think of it as a size that the developers of your compiler decided would be a convenient default. Usually, this just means that it's the best-supported on the assembly-language level. The only obstacles to using a larger integer type, then, are (1) the additional work required to compile it to assembly language, and (2) the performance penalties that you thereby incur.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    The same way a 16 bit machine supports 32 bit data types - by using two machine words to represent the value.

    While a 32 bit machine can handle 32 bit data directly by for example being able to add two values in a single instruction, when handling 64 bit data multiple instructions are required and these are automatically generated by the compiler.

    So for example for add, the compiler would generate code to add the two least significant words, and if this generates an overflow indicated by testing the carry flag, it would add one to the most significant work of the result. That is perhaps three instructions rather than one.

IMN logo majestic logo threadwatch logo seochat tools logo