Reputation: 1135
When we perform arithmetic operation on 8 bit numbers, they are promoted to integers. I have below code snippet.
unsigned char startTime=7;
unsigned char endTime=5;
unsigned char diff = endTime-startTime;
I understand that, in RHS, both are promoted to integers and hence the result is -2 and final result is 254. I want to know how exactly compiler does this. This is what I felt, but not sure. Compiler promotes both of them to integers. Temporary integer variable is created for the result. Arithmetic operation is performed. Result of 5-7 = 0xFFFF FFFF FFFF FFFE . Now assign only 8 bits 0xFE to LHS and all higher 3 bytes are stripped off. Hence we see 254 if we print it.
Upvotes: 1
Views: 555
Reputation: 222273
As you note, startTime
and endTime
are promoted to int
, and the result of endTime-startTime
is −2. The C standard describes this behavior in terms of the value that results, which is −2. It does not describe the behavior in terms of the bits that encode the result; it does not mandate that the result be represented by the bits 11111111111111111111111111111110.
In many C implementations, essentially all common modern implementations, the two’s complement system is used for signed integer, in which the 32-bit representation for −2 is 11111111111111111111111111111110. But the C standard also allows using one’s complement (in which −2 is 11111111111111111111111111111101) or sign-and-magnitude (in which −2 is 10000000000000000000000000000010).
Regardless of which system is used, the result of converting −2 to an eight-bit unsigned char
is 254. (C allows unsigned char
to be wider than eight bits, but this answer does not address that.) This is because the C standard specifies that the result of converting an integer value to unsigned char
is the value in the domain of the unsigned char
type that is obtained by adding or subtracting UCHAR_MAX+1
repeatedly. That is, it is the value being converted modulo 256. For −2, we have −2 + 256 = 254, so the result required by the C standard is 254.
Since this result is required by the C standard requires of whether two’s complement, one’s complement, or sign-and-magnitude is used, the C compiler (or entire C implementation) is responsible for doing whatever is necessary to get this result. When two’s complement is used, all the compiler has to do is take the low eight bits of the int
representation and use them as the bits of the unsigned char
representation. This is one of the reasons two’s complement is favored today. If the C implementation were using one’s complement or sign-and-magnitude, it would be responsible for doing whatever work is necessary to convert the −2 int
value into a 254 unsigned char
value.
Upvotes: 1