Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What aspects of signed left shift are undefined with GCC?

Tags:

c

gcc

bit-shift

Basically I am asking for a translation of:

GCC does not use the latitude given in C99 only to treat certain aspects of signed `<<' as undefined, but this is subject to change.

(GCC 4.8.1 manual, paragraph 4.5)

What 'latitude' is given? Which certain aspects?

like image 921
maxschlepzig Avatar asked Aug 24 '13 03:08

maxschlepzig


Video Answer


1 Answers

Other answers have already pointed out which aspects of << are undefined behavior. My guess is that you want a "translation" of the gcc into common language.

If a behavior is undefined by the C standard, compiler implementors can take the "latitude" to do anything that suits them if such a case occurs. In particular, they don't have to implement a diagnostic or detection of that case, and may pretend that it never happens. It is the responsability of the programmer to write his program such that its behavior is always defined.

In the case of left shift, this means that a compiler would not have to check for overflow and can pretend that a loop like

for (int i = 1; i > 0; i <<= a) {
 .... change a in some complicated way ...
}

would never terminate.

The sentence that you are citing indicates that they don't do such a thing, yet, but that future versions of gcc might do so.

like image 83
Jens Gustedt Avatar answered Sep 30 '22 12:09

Jens Gustedt