씨언어어서 lhs << rhs 했을때, rhs의 값이 lhs의 비트수

seoleda의 이미지

그냥.. 갑자기 궁금증이 생겨서 해봤는데요..

lhs << rhs 했을 경우에, rhs의 값이 lhs의 비트수보다 클 경우에 rhs%(lhs의 비트수) 의 값만큼 shift가 되더군요..

그런데, 이게 표준에 있는 행동인가요? 아님 gcc에서만 그렇게 동작하는 건가요? ^^

singlet의 이미지

C99 6.5.7.3 에서

Quote:
If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

C++98 5.8.1 에서

Quote:
The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.