Crate avr_int24

Crate avr_int24 

Source
Expand description

§24 bit integer arithmetic for AVR

This library provides a 24-bit signed integer type, Int24, for Rust. It is designed for use on AVR microcontrollers.

No operation from this crate ever panics.

The operations don’t overflow or underflow. Numeric limits are handled by saturating the result instead. The only exception is the left shift operation, which does not saturate.

Here are some example uses:

use avr_int24::Int24;

let a = Int24::from_i16(30_000);
let b = Int24::from_i16(10_000);

// Addition
let c = a + b;
assert_eq!(c.to_i32(), 40_000);

// Subtraction
let c = b - a;
assert_eq!(c.to_i32(), -20_000);

// Multiplication
let c = a * Int24::from_i16(-10);
assert_eq!(c.to_i32(), -300_000);

// Division
let c = a / b;
assert_eq!(c.to_i32(), 3);

// Negation
let c = -a;
assert_eq!(c.to_i32(), -30_000);

// Arithmetic right shift
let c = a >> 2;
assert_eq!(c.to_i32(), 7_500);

// Left shift
let c = a << 2;
assert_eq!(c.to_i32(), 120_000);

// Saturation
let c = a * b;
assert_eq!(c.to_i32(), 0x7F_FFFF);
let c = a * -b;
assert_eq!(c.to_i32(), -0x80_0000);

Structs§

Int24
24 bit signed integer.

Type Aliases§

I24
Shorthand for Int24.
Int24Raw
Raw bytes tuple; Little Endian.