Reputation: 2653
int exp1 = ((1<<31)>>31)<<32 // output changes everytime
int exp2 = ((1<<31)>>31)<<31<<1 // 0
why is this happening?
it may be caused by overflow thing, but cannot understand properly.
I am trying to solve this problem for hours, need some help
(p.s integer for 32bits)
Upvotes: 0
Views: 61
Reputation: 126867
Shifting by the whole type size or more is undefined behavior, so anything can happen (it comes from the fact that many architectures shift instructions have bizarre behavior in these cases). Splitting the shift in two parts works around the issue.
Upvotes: 3