What is going on with bitwise operators and integer promotion?

[expr.unary.op] The operand of ~ shall have integral or unscoped enumeration type; the result is the one’s complement of its operand. Integral promotions are performed. [expr.shift] The shift operators << and >> group left-to-right. […] The operands shall be of integral or unscoped enumeration type and integral promotions are performed. What’s the integral promotion of uint8_t … Read more

unsigned int and signed char comparison

Section 6.3.1.8, Usual arithmetic conversions, of C99 details implicit integer conversions. If both operands have the same type, then no further conversion is needed. That doesn’t count since they’re different types. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank … Read more

Overflowing of Unsigned Int

unsigned numbers can’t overflow, but instead wrap around using the properties of modulo. For instance, when unsigned int is 32 bits, the result would be: (a * b) mod 2^32. As CharlesBailey pointed out, 253473829*13482018273 may use signed multiplication before being converted, and so you should be explicit about unsigned before the multiplication: unsigned int … Read more

A warning – comparison between signed and unsigned integer expressions

It is usually a good idea to declare variables as unsigned or size_t if they will be compared to sizes, to avoid this issue. Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_type when comparing with a std::string‘s length). Compilers give warnings about comparing signed and unsigned types because … Read more

Why is size_t unsigned?

size_t is unsigned for historical reasons. On an architecture with 16 bit pointers, such as the “small” model DOS programming, it would be impractical to limit strings to 32 KB. For this reason, the C standard requires (via required ranges) ptrdiff_t, the signed counterpart to size_t and the result type of pointer difference, to be … Read more

Why are unsigned int’s not CLS compliant?

Not all languages have the concept of unsigned ints. For example VB 6 had no concept of unsigned ints which I suspect drove the decision of the designers of VB7/7.1 not to implement as well (it’s implemented now in VB8). To quote: http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx The CLS was designed to be large enough to include the language … Read more