2

Take a look at the following snippet:

#include <iostream>
#include <cstdint>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
int main(int argc, char** argv)
{
        constexpr uint16_t b = 2;
        constexpr uint16_t c = 3;
        constexpr const auto bc = b * c;
        std::cout << "b: " << type_id_with_cvr<decltype(b)>().pretty_name() << std::endl;
        std::cout << "b * c: " << type_id_with_cvr<decltype(bc)>().pretty_name()  << std::endl;
}

This results in the following:

b: unsigned short const

b * c: int const

Why does multiplying two unsinged ints results in an integer?

Compiler: g++ 5.4.0

bergercookie
  • 2,542
  • 1
  • 30
  • 38

1 Answers1

3

unsigned short values are implicitly converted to an int before the multiplication.

short and char are considered "storage types" and implicitly converted to int before doing computations. It's the reason for which

unsigned char x = 255, y = 1;
printf("%i\n", x+y); // you get 256, not 0
6502
  • 112,025
  • 15
  • 165
  • 265