LOGBOOK

HELP

Quiz Entry - updated: 2026.07.07

How do you fix signed division by power of 2 to round toward zero?

Add a bias of (2^k − 1) to negative values before shifting right, so the result rounds toward zero like C's / instead of toward −∞.

Formula: (x + (1 << k) - 1) >> k for x < 0

// Correct signed division by 2^k
int divide_power2(int x, int k) {
    int bias = (x < 0) ? (1 << k) - 1 : 0;
    return (x + bias) >> k;
}

Example: -7 ÷ 4 (k=2)

Without bias: -7 >> 2 = -2 (wrong, rounds to -∞)
Bias = (1 << 2) - 1 = 3
With bias: (-7 + 3) >> 2 = -4 >> 2 = -1 ✓

Why: The bias rounds negative numbers "up" (toward zero) before the shift rounds them "down."

From Quiz: REVE1 / Number Representations | Updated: Jul 07, 2026