Why is such complex code emitted for dividing a signed integer by a power of two?

The reason is that unsigned division by 2^n can be implemented very simply, whereas signed division is somewhat more complex.

unsigned int u;
int v;

u / 4096 is equivalent to u >> 12 for all possible values of u.

v / 4096 is NOT equivalent to v >> 12 – it breaks down when v < 0, as the rounding direction is different for shifting versus division when negative numbers are involved.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)