#351 Please consider making compile-time shift calculation more consistent with math definition

4.7.x_development
open
nobody
None
2
2013-12-01
2013-04-22
No

Following code produces unexpected result (0) for BITS=16:

#define BITS 16
#define MOD(x) ((x) & ((1 << BITS) - 1))

int v = 5;
int main()
{
    printf("%d\n", MOD(v));
}

More discussion is at https://sourceforge.net/mailarchive/message.php?msg_id=30751567

Despite shifts to values bigger than bit size of type are undefined per C standard, there're:

1) Arithmetical definition of shift operation;
2) Behavior of other well-known gcc ports (x86);
3) User expectations;
4) Principle of least surprise.

So, it would be nice to change compile-time (and only compile-time!) behavior to more consistent with mathematical definition.

This is obviously not a priority request.

Discussion


Log in to post a comment.