Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to workaround warning C4333 ('>>' : right shift by too large amount, data loss)

I have the following function to convert an integer of arbitrary size to a buffer:

template<typename T>
std::string build_data_from(T val)
{
  std::string result;

  for (int i = 0; i < sizeof(val); i++)
  {
    result.insert(0, 1, char(val));
    val = val >> 8;
  }

  return result;
};

However, invoking the template function with an unsigned char renders a warning in Visual C++ 2008:

std::string x(build_data_from<unsigned char>(1));

warning C4333: '>>' : right shift by too large amount, data loss

Is there any clean way (without using a pragma warning directive) to workaround it?

like image 329
Rômulo Ceccon Avatar asked Nov 18 '25 00:11

Rômulo Ceccon


1 Answers

The following will get rid of he warning.

change

val = val >> 8;

to

val = val >> 7 >> 1;

or

val = (val >> 7 >> 1) & 0xff;
like image 184
zoubin Avatar answered Nov 20 '25 15:11

zoubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!