Unsigned sub-thread mechanism

I know the following

unsigned short b=-5u; 

estimates the value of b equal to 65531 due to insufficient flow, but I do not understand if 5u is converted to a signed int before converting to -5 and then converted back to unsigned again for storage in b or -5u. equal to 0 - 5u (it should not be, -x is a unary operator)

+4
source share
4 answers

5u is an unsigned literal integer, -5u is its negation. Negation for unsigned integers is defined as a subtraction from 2 ** n, which gets the same result as zeroing the result of subtraction from zero.

+5
source

5u is a single token, an rvalue expression of type unsigned int . Unary operator - applied to it in accordance with the rules of unsigned arithmetic (arithmetic modulo 2 ^ n, where n is the number of bits in an unsigned type). Results are converted to unsigned short ; if they are not fit (and they will not, if sizeof(int) > sizeof(short) ), the conversion will be performed modulo arithmetic (modulo 2 ^ n, where n is the number of bits in the target type).

It may be worth noting that if the original argument is unsigned short , the actual steps are different (although the results will always be the same). Thus, if you would write:

 unsigned short k = 5; unsigned short b = -k; 

The first operation will depend on the size of the short. If the shorts are smaller than ints (often, but not always), the first step would be to advance k to int . (If the sizes of short and int are identical, then the first step would be to advance k to unsigned int ; since then everything happens as described above.) Unary - will be applied to this int , according to the rule of signed integer arithmetic (thus, leading to the value -5). the result of -5 will be implicitly converted to unsigned short using modulo arithmetic as described above.

In general, these differences do not matter, but in cases where you can have the integral value INT_MIN , they could; for 2 additions of machines, -i , where i is of type int , and the value of INT_MIN is an implementation and may lead to strange values โ€‹โ€‹during subsequent conversion to unsigned.

+3
source

ISO / IEC 14882-2003 clause 4.7 states:

โ€œIf no destination type is specified, the resulting value is the smallest unsigned integer comparable to the original integer (modulo 2 ^ n, where n is the number of bits used to represent the unsigned type). [Note: in binary, this conversion is conceptual and there is no change in the bitmap (if there is no truncation). -end note]

+1
source

Technically, not an overflow, but simply a representation of the -5 value when it appears as an unsigned number. Please note that signed and unsigned numbers have the same "bits" - they just display differently. If you were to print the value as a signed value [assuming it was expanded with a character bit to fill in the remaining bits], it will display -5. [This suggests that this is a typical machine using the 2s add-on. The C standard does not require that the signed and unsigned types have the same number of bits, and that the computer uses the 2s pad to represent signed numbers - obviously, if it does not use the 2s pad, it will not match the value you showed, so I made the assumption that your IS-machine complements 2s - these are all common processors, such as x86, 68K, 6502, Z80, PDP-11, VAX, 29K, 8051, ARM, MIPS, But technically, it is not necessary that C function right]

And when you use the unary operator -x , it has the same effect as 0-x [this applies to both computers and math โ€” it has the same result].

0
source

All Articles