简体   繁体   中英

How can signed integers be converted to unsigned integers

I am reading accelerated c++ and I just came across a line:

Vector.size() -100 yield unsigned results, which means that they, too, cannot be less than zero- even if vector.size() < 100.

What is happening here? Any help is appreciated. Thank you

Vector.size() -100

What is happening here?

One of the operands to the arithmetic expression is signed, and the other operand is unsigned. Moreover, the rank of the unsigned type is likely higher than the rank of the signed operand.

In this case, the signed operand is converted to the unsigned type of higher rank. Since 100 is representable in the unsigned type, this does not affect the value.

The subtraction is performed on the unsigned operands. If the mathematical result of the unsigned operation would be outside of the representable range (ie negative), then the actual result would be the representable value that is congruent with the mathematical result modulo M, where M is the number of representable values in the unsigned type.

Since only positive values are in the representable range of unsigned integers, the actual result that is congruent with the mathematical result must also be positive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM