Skip to main content

twos_complement

Function twos_complement 

Source
pub fn twos_complement(value: u64, width: u32) -> i64
Expand description

Reinterprets an unsigned field of width bits as a signed integer.

Sign-extends by shifting rather than by subtracting 2^width. The subtraction form overflows at width 63 — 1i64 << 63 is i64::MIN, so positive - i64::MIN wraps — which release builds happen to get right and debug builds panic on, so no differential test against real packets would ever have caught it. Shifting is branchless and correct for every width from 1 to 64.