I've problem with the size of long int
on a 16-bit CPU. Looking at its architecture:
No register is more than 16-bit long. So, how come long int
can have more than 16bits. In fact, according to me for any Processor, the maximum size of the data type must be the size of the general purpose register. Am I right?
Yes. In fact the C and C++ standards require that sizeof(long int) >= 4
.*
(I'm assuming CHAR_BIT == 8
in this case.)
This is the same deal with 64-bit integers on 32-bit machines. The way it is implemented is to use two registers to represent the lower and upper halves.
Addition and subtraction are done as two instructions:
On x86:
add
and adc
where adc
is "add with carry"sub
and sbb
where sbb
is "subtract with borrow"For example:
long long a = ...;
long long b = ...;
a += b;
will compile to something like:
add eax,ebx
adc edx,ecx
Where eax
and edx
are the lower and upper parts of a
. And ebx
and ecx
are the lower and upper parts of b
.
Multiplication and division for double-word integers is more complicated, but it follows the same sort of grade-school math - but where each "digit" is a processor word.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With