0

A silly question. I just made a few tests. My expectation was that after:

uint32_t v = ~ (uint32_t) 0;
v <<= sizeof(uint32_t) * 8;

or

size_t v = ~ (size_t) 0;
v <<= sizeof(size_t) * 8;

v shall be zero. But it's not - it's equal to original value. So for 64-bit every shift up to 63 does shift bits, but shift by 64 does nothing (equal to shift by 0).
This is controversial for me.

Is this compiler specific or by C++ standard? I'm trying gcc version 8.3.0 under linux. Params are: g++ -c -pipe -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC

ruohola
  • 19,113
  • 6
  • 51
  • 82
truf
  • 2,504
  • 22
  • 39

1 Answers1

1

You are knee-deep in undefined behavior:

In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.

https://en.cppreference.com/w/cpp/language/operator_arithmetic

SergeyA
  • 59,974
  • 5
  • 72
  • 130