Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 5th, 2013, 12:00 AM
 Avichal
Contributing User

Join Date: Dec 2012
Posts: 69
Time spent in forums: 8 h 21 m 31 sec
Reputation Power: 1
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
January 5th, 2013, 12:18 AM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 5 h 3 m 14 sec
Reputation Power: 4106
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

#3
January 5th, 2013, 12:43 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 23 h 42 m 26 sec
Reputation Power: 2199
Quote:
 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.

#4
January 5th, 2013, 01:13 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
Quote:
 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.

#5
January 5th, 2013, 05:54 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,961
Time spent in forums: 1 Month 4 Days 2 h 35 m 15 sec
Reputation Power: 1801
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > How long long int works?