Expand description

Traits for finding the remainder of a number divided by $2^k$, subject to various rounding rules.

These are the traits:

roundingby value or referenceby mutable reference (assignment)
towards $-\infty$ModPowerOf2ModPowerOf2Assign
towards 0RemPowerOf2RemPowerOf2Assign
towards $\infty$CeilingModPowerOf2CeilingModPowerOf2Assign
towards $\infty$NegModPowerOf2NegModPowerOf2Assign

CeilingModPowerOf2 and NegModPowerOf2 are similar. The difference is that CeilingModPowerOf2 returns a remainder less than or equal to 0, so that the usual relation $x = q2^k + r$ is satisfied, while NegModPowerOf2 returns a remainder greater than or equal to zero. This allows the remainder to have an unsigned type, but modifies the relation to $x = q2^k - r$.

mod_power_of_2

use malachite_base::num::arithmetic::traits::ModPowerOf2;

// 1 * 2^8 + 4 = 260
assert_eq!(260u16.mod_power_of_2(8), 4);

// 100 * 2^4 + 11 = 1611
assert_eq!(1611u32.mod_power_of_2(4), 11);

// 1 * 2^8 + 4 = 260
assert_eq!(260i16.mod_power_of_2(8), 4);

// -101 * 2^4 + 5 = -1611
assert_eq!((-1611i32).mod_power_of_2(4), 5);

mod_power_of_2_assign

use malachite_base::num::arithmetic::traits::ModPowerOf2Assign;

// 1 * 2^8 + 4 = 260
let mut x = 260u16;
x.mod_power_of_2_assign(8);
assert_eq!(x, 4);

// 100 * 2^4 + 11 = 1611
let mut x = 1611u32;
x.mod_power_of_2_assign(4);
assert_eq!(x, 11);

// 1 * 2^8 + 4 = 260
let mut x = 260i16;
x.mod_power_of_2_assign(8);
assert_eq!(x, 4);

// -101 * 2^4 + 5 = -1611
let mut x = -1611i32;
x.mod_power_of_2_assign(4);
assert_eq!(x, 5);

rem_power_of_2

use malachite_base::num::arithmetic::traits::RemPowerOf2;

// 1 * 2^8 + 4 = 260
assert_eq!(260u16.rem_power_of_2(8), 4);

// 100 * 2^4 + 11 = 1611
assert_eq!(1611u32.rem_power_of_2(4), 11);

// 1 * 2^8 + 4 = 260
assert_eq!(260i16.rem_power_of_2(8), 4);

// -100 * 2^4 + -11 = -1611
assert_eq!((-1611i32).rem_power_of_2(4), -11);

rem_power_of_2_assign

use malachite_base::num::arithmetic::traits::RemPowerOf2Assign;

// 1 * 2^8 + 4 = 260
let mut x = 260u16;
x.rem_power_of_2_assign(8);
assert_eq!(x, 4);

// 100 * 2^4 + 11 = 1611
let mut x = 1611u32;
x.rem_power_of_2_assign(4);
assert_eq!(x, 11);

// 1 * 2^8 + 4 = 260
let mut x = 260i16;
x.rem_power_of_2_assign(8);
assert_eq!(x, 4);

// -100 * 2^4 + -11 = -1611
let mut x = -1611i32;
x.rem_power_of_2_assign(4);
assert_eq!(x, -11);

neg_mod_power_of_2

use malachite_base::num::arithmetic::traits::NegModPowerOf2;

// 2 * 2^8 - 252 = 260
assert_eq!(260u16.neg_mod_power_of_2(8), 252);

// 101 * 2^4 - 5 = 1611
assert_eq!(1611u32.neg_mod_power_of_2(4), 5);

neg_mod_power_of_2_assign

use malachite_base::num::arithmetic::traits::NegModPowerOf2Assign;

// 2 * 2^8 - 252 = 260
let mut x = 260u16;
x.neg_mod_power_of_2_assign(8);
assert_eq!(x, 252);

// 101 * 2^4 - 5 = 1611
let mut x = 1611u32;
x.neg_mod_power_of_2_assign(4);
assert_eq!(x, 5);

ceiling_mod_power_of_2

use malachite_base::num::arithmetic::traits::CeilingModPowerOf2;

// 2 * 2^8 + -252 = 260
assert_eq!(260i16.ceiling_mod_power_of_2(8), -252);

// -100 * 2^4 + -11 = -1611
assert_eq!((-1611i32).ceiling_mod_power_of_2(4), -11);

ceiling_mod_power_of_2_assign

use malachite_base::num::arithmetic::traits::CeilingModPowerOf2Assign;

// 2 * 2^8 + -252 = 260
let mut x = 260i16;
x.ceiling_mod_power_of_2_assign(8);
assert_eq!(x, -252);

// -100 * 2^4 + -11 = -1611
let mut x = -1611i32;
x.ceiling_mod_power_of_2_assign(4);
assert_eq!(x, -11);