I am having a hard time understanding the output of the following program.
int main(){
char y = 0x02;
y = y-0x03;
printf("0x%x %d",y,sizeof(y));
return 0;
}
The output that I am getting is
0xffffffff 1
I know so far is that if I add two hexadecimal values their binary values are added. The same is done for subtraction using the concept of borrow (please correct me if I am wrong)
Examples
111 - 001 = 110 (in binary) 110 - 001 = 101 (using carry from second LSB in first operand)
In the case of addition of two hexadecimal values if the values overflow char
they are basically mod 256. That is if I write a program as
int main(){
char y = 0x03;
y = y+0xff;
printf("0x%x",y);
return 0;
}
I will get output of
0x2
But in case of subtraction, if I run the first program, the expected output should be
0xff
So my question is since the above subtraction should be
00000010 - 00000011
in binary, where the borrow is coming from? and why I am getting this
0xffffffff
weird output?