1

When does bit shifting cause undefined behavior in C++? And does the behavior differ between C++ versions and integer types?

palapapa
  • 573
  • 2
  • 5
  • 25
  • Does this answer your question? [What are bitwise shift (bit-shift) operators and how do they work?](https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work) – Nathan Pierson Jul 15 '23 at 19:54
  • @NathanPierson No, my question is specifically about the undefined behavior of bit shifting in C++. The question you linked is language-agnostic. Someone mentioned C++ in the answers but did not mention C++20's behavior. – palapapa Jul 15 '23 at 20:12

1 Answers1

3

Before C++20

Positive signed integer

  • Left shift: Shifted-out bits get discarded and the least significant bits are filled with zeros. If the shifted-out bits are not all zeros, the behavior is undefined.

  • Right shift: Shifted-out bits get discarded and the most significant bits are filled with zeros.

Negative signed integer

  • Left shift: Undefined behavior.

  • Right shift: Implementation defined. Usually for 2's complement systems, the shifted-out bits get discarded, and the most significant bits are filled with ones.

Unsigned integer

  • Left shift: Shifted-out bits get discarded and the least significant bits are filled with zeros.

  • Right shift: Shifted-out bits get discarded and the most significant bits are filled with zeros.

After C++20

Positive signed integer

  • Left shift: Shifted-out bits get discarded and the least significant bits are filled with zeros.

  • Right shift: Shifted-out bits get discarded and the most significant bits are filled with zeros.

Negative signed integer

  • Left shift: Shifted-out bits get discarded and the least significant bits are filled with zeros.

  • Right shift: Shifted-out bits get discarded, and the most significant bits are filled with ones.

Unsigned integer

  • Left shift: Shifted-out bits get discarded and the least significant bits are filled with zeros.

  • Right shift: Shifted-out bits get discarded and the most significant bits are filled with zeros.

In all cases, if the number of bits to shift(the right operand) is greater than or equal to the number of bits of the left operand after integer promotion, the behavior is undefined.

palapapa
  • 573
  • 2
  • 5
  • 25
  • This feels very much like something copied from a text book or other source. As well as an attempt to gain some reputation for information that is readily available from multiple sources (and probably have plenty of duplicates here on SO already). – Some programmer dude Jul 15 '23 at 17:30
  • 2
    @Someprogrammerdude I wrote this post myself and have not found a question like this on SO, so I decided to summarize this issue for myself and for others as well for future reference. Could you give me an example of a duplicate? – palapapa Jul 15 '23 at 17:32
  • 3
    What does this add over, say, [the cppreference page](https://en.cppreference.com/w/cpp/language/operator_arithmetic#Bitwise_shift_operators) on shift operators? Maybe there's no questions about this because the information is already well-documented at a reputable reference site. – Nathan Pierson Jul 15 '23 at 17:37
  • 1
    @NathanPierson cppreference described it with modulo, exponentiation, and division, and I thought it might be easier for others to understand if it is presented as actually shifting bits instead of math. – palapapa Jul 15 '23 at 17:41
  • @JesperJuhl I literally wrote it in the last sentence. – palapapa Jul 15 '23 at 18:00
  • 2
    @NathanPierson Come on, 4 out of 5 questions here are trivially answered by some reference site. – DevSolar Jul 15 '23 at 18:04
  • 2
    @DevSolar not only that, the goal of Stack Overflow is to be a *complete* compendium of answers. Answers being available elsewhere have no bearing on whether they should be here. – Mark Ransom Jul 15 '23 at 18:17