Signed versus Unsigned Integers

Unsigned can hold a larger positive value and no negative value. Yes. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to identify if the number is positive or negative. There are different ways of representing signed integers. The easiest to visualise is to use the … Read more

What happens if I assign a negative value to an unsigned variable?

For the official answer – Section 4.7 conv.integral “If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there … Read more

Signed/unsigned comparisons

When comparing signed with unsigned, the compiler converts the signed value to unsigned. For equality, this doesn’t matter, -1 == (unsigned) -1. For other comparisons it matters, e.g. the following is true: -1 > 2U. EDIT: References: 5/9: (Expressions) Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result … Read more

Comparison operation on unsigned and signed integers

Binary operations between different integral types are performed within a “common” type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8). In your case the “common” type is unsigned int. This means that int operand (your b) will get converted to unsigned int before the comparison, as well as for the purpose … Read more