This code triggers a warning with -Werror=conversion
(GCC 10.1), and I don't understand why:
std::uint16_t i = 24, j = 12, k = 13;
std::uint16_t x = (i >> j) & k;
error: conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Werror=conversion]
According to cppreference, the return type of the right shift operator is simply the type of its LHS, which should be std::uint16_t
. Adding two std::uint16_t
should produce another std::uint16_t
, so why am I getting a warning?
Note that everything works just fine if I do this:
std::uint16_t i = 24, j = 12, k = 13;
std::uint16_t x = (i >> j);
std::uint16_t y = x & k;