NO

Author Topic: No warning when left-shifting in signed bit  (Read 565 times)

Alad

  • Guest
No warning when left-shifting in signed bit
« on: August 06, 2016, 12:59:42 am »
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

signed int main(void)
{
/* Two's complement */
signed char schar_min = 1;
schar_min <<= ((sizeof(signed char) * CHAR_BIT) - 1);

printf("%d\n", schar_min);

return EXIT_SUCCESS;
}

The expression in schar_min is undefined, as it shifts into a negative value, from 0000 0001 (1) to 1000 0000 (-128). The program however compiles without warnings.

See C11, 6.5.7.4):

Quote
The result of 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 × 2 E2 , reduced modulo
one more than the maximum value representable in the result type. If E1 has a signed
type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.

Platform: Windows 8.1, x86_64
Version: Pelles C 8.00.60

Alad

  • Guest
Re: No warning when left-shifting in signed bit
« Reply #1 on: August 06, 2016, 02:20:27 am »
Pelles C warns on other occasions on undefined behaviour, such as shifting int outside it's width, as do other compilers like clang. Whether you want to ignore those warnings or not doesn't matter here.

Scripter

  • Guest
Re: No warning when left-shifting in signed bit
« Reply #2 on: August 06, 2016, 04:20:37 pm »
Pelles C warns on other occasions on undefined behaviour, such as shifting int outside it's width, as do other compilers like clang. Whether you want to ignore those warnings or not doesn't matter here.


Ok... message removed. Good luck.