+#[derive(Debug, Default, Copy, Clone)]
+struct MixBit {
+ v: u8,
+}
+
+impl MixBit {
+ // Clamps a u8 to a u6
+ fn value(&self) -> u8 {
+ return self.v % 64;
+ }
+
+ fn as_value(&self) -> MixBit {
+ MixBit { v: self.value() }
+ }
+}
// > Each byte contains an unspecified amount of information but it must be capable of holding at
// > least 64 distinct values.
// > A computer word consists of five bytes and a sign. The sign portion has only two possible
#[derive(Debug, Default, Copy, Clone)]
struct Word {
sign: bool,
- bytes: [u8; 5],
+ bytes: [MixBit; 5],
}
impl Word {
- fn new(sign: bool, bytes: [u8; 5]) -> Word {
+ fn new(sign: bool, bytes: [MixBit; 5]) -> Word {
+ Word { sign, bytes }
+ }
+ // The allowable fields are those that are adjacent in a computer word, and they are
+ // represented by (L:R) where L is the number of the left hand part and R is the number of the
+ // right-hand part of the field.
+ fn field_specification(&self, l: usize, r: usize) -> Word {
+ let mut sign = false;
+ if l == 0 {
+ sign = self.sign;
+ }
+ let r_clamp = r % 5;
+ let mut bytes = [MixBit::default(); 5];
+ for n in l..r_clamp {
+ bytes[n] = self.bytes[n];
+ }
Word { sign, bytes }
}
}
}
fn main() {
- println!("{:?}", Machine::default());
+ println!(
+ "{:?}",
+ Word {
+ sign: true,
+ bytes: [MixBit { v: 255 }; 5]
+ }
+ .field_specification(0, 2)
+ );
}