将 0 向左移动是否被认为是 C 中的未定义行为?

问题描述 投票:0回答:1

是否是未定义行为(UB):

  1. 0<< 32
  2. 0LL<< 64

如果答案包含对 C 标准的引用,我将不胜感激。

c language-lawyer standards undefined-behavior
1个回答
0
投票

请参阅 ISO C 标准 (ISO/IEC 9899),6.5.7 位移运算符,第 4 段:

E1结果<< E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

0

<<
32 和 0LL
<<
64 都会导致 C 中的未定义行为 (UB)。

© www.soinside.com 2019 - 2024. All rights reserved.