diff --git a/src/common.rs b/src/common.rs index 1b448e2..c9ba335 100644 --- a/src/common.rs +++ b/src/common.rs @@ -17,7 +17,7 @@ where { pub fn new(precision: u32) -> Self { let low = B::ZERO; - let high = B::ONE << precision; + let high = (B::ONE << precision) - B::ONE; Self { precision, diff --git a/src/decoder.rs b/src/decoder.rs index 46f8dd9..09c28ce 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -209,13 +209,13 @@ where fn normalise(&mut self) -> io::Result<()> { while self.state.high < self.state.half() || self.state.low >= self.state.half() { if self.state.high < self.state.half() { - self.state.high <<= 1; + self.state.high = (self.state.high << 1) + B::ONE; self.state.low <<= 1; self.x <<= 1; } else { // self.low >= self.half() self.state.low = (self.state.low - self.state.half()) << 1; - self.state.high = (self.state.high - self.state.half()) << 1; + self.state.high = ((self.state.high - self.state.half()) << 1) + B::ONE; self.x = (self.x - self.state.half()) << 1; } @@ -228,7 +228,7 @@ where && self.state.high < (self.state.three_quarter()) { self.state.low = (self.state.low - self.state.quarter()) << 1; - self.state.high = (self.state.high - self.state.quarter()) << 1; + self.state.high = ((self.state.high - self.state.quarter()) << 1) + B::ONE; self.x = (self.x - self.state.quarter()) << 1; if self.input.next_bit()? == Some(true) { diff --git a/src/encoder.rs b/src/encoder.rs index 8594407..b8782c0 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -201,12 +201,12 @@ where while self.state.high < self.state.half() || self.state.low >= self.state.half() { if self.state.high < self.state.half() { self.emit(false)?; - self.state.high <<= 1; + self.state.high = (self.state.high << 1) + B::ONE; self.state.low <<= 1; } else { self.emit(true)?; self.state.low = (self.state.low - self.state.half()) << 1; - self.state.high = (self.state.high - self.state.half()) << 1; + self.state.high = ((self.state.high - self.state.half()) << 1) + B::ONE; } } @@ -215,7 +215,7 @@ where { self.pending += 1; self.state.low = (self.state.low - self.state.quarter()) << 1; - self.state.high = (self.state.high - self.state.quarter()) << 1; + self.state.high = ((self.state.high - self.state.quarter()) << 1) + B::ONE; } Ok(())