I Learned About 2's Complement
and unsigned and signed int. So I Decided to test my knowledge , as far as i know that a negative number is stored in 2's complement
way so that addition and subtraction would not have different algorithm and circuitry would be simple.
Now If I Write
int main()
{
int a = -1 ;
unsigned int b = - 1 ;
printf("%d %u
%d %u" , a ,a , b, b);
}
Output Comes To Be -1 4294967295 -1 4294967295
. Now , i looked at the bit pattern and various things and then i realized that -1
in 2's complement is 11111111 11111111 11111111 11111111
, so when i interpret it using %d , it gives -1
, but when i interpret using %u
, it treat it as a positive number and so it gives 4294967295
. I Checked the Assembly Of the code is
.LC0:
.string "%d %u
%d %u"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], -1
mov DWORD PTR [rbp-8], -1
mov esi, DWORD PTR [rbp-8]
mov ecx, DWORD PTR [rbp-8]
mov edx, DWORD PTR [rbp-4]
mov eax, DWORD PTR [rbp-4]
mov r8d, esi
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov eax, 0
leave
ret
Now here -1
is moved to the register both the times in unsigned and signed . What i want to know if reinterpretation is only that matters , then why do we have two types unsigned
and signed
, it is printf
format string %d
and %u
that matters ?
Further what really happens when i assign negative number to a unsigned integer (I learned That The initializer converts this value from int
to unsigned int
. ) but in the assembly code I did not saw such a thing. So what really happens ??
And How does Machine knows when it has to do 2's complement
and when not , does it see the negative sign and performs 2's complement
?
I have read almost every question and answer you could think this question be duplicate of , but I could not find a satisfactory solution.
See Question&Answers more detail:
os