The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How long long int works?
Discuss How long long int works? in the C Programming forum on Dev Shed. How long long int works? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 4th, 2013, 11:00 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 19
Time spent in forums: 3 h 51 m 41 sec
Reputation Power: 0
|
|
|
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?
|

January 4th, 2013, 11:18 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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
|

January 4th, 2013, 11:43 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

January 5th, 2013, 12:13 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
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.
|

January 5th, 2013, 04:54 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|