From c730e0d1bdffded978d064ffba111a5f119a5be7 Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Mon, 26 Jun 2023 00:15:07 +0300 Subject: [PATCH] Rusty implementation. --- Cargo.toml | 16 +++++++ README.md | 0 src/bit_utilities.rs | 101 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 44 +++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/bit_utilities.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8e99260 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rust_tests" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[profile.release] +opt-level = 'z' # Optimize for size +lto = true # Enable link-time optimization +codegen-units = 1 # Reduce number of codegen units to increase optimizations +panic = 'abort' # Abort on panic +strip = true # Strip symbols from binary* + +[dependencies] +text_io = "0.1.12" \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/bit_utilities.rs b/src/bit_utilities.rs new file mode 100644 index 0000000..3776a5b --- /dev/null +++ b/src/bit_utilities.rs @@ -0,0 +1,101 @@ +use std::fmt; +use std::fmt::Formatter; + +fn capitalise(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + None => String::new(), + Some(f) => f.to_uppercase().collect::() + c.as_str(), + } +} + +/// A basic register capable of storing binary data. +#[derive(Debug)] +pub struct BasicRegister { + /// A [Vec] that stores the binary data of the register. + memory: Vec, +} + +impl BasicRegister { + pub fn l_shift(&mut self) { + let mut res: Vec = vec![false; self.memory.len()]; + + res[..(self.memory.len() - 1)] + .copy_from_slice(&self.memory[1..((self.memory.len() - 1) + 1)]); + + self.memory = res; + } + + pub fn r_shift(&mut self) { + let mut res: Vec = vec![false; self.memory.len()]; + + for i in (1..self.memory.len()).rev() { + res[i] = self.memory[i - 1]; + } + + self.memory = res; + } + + pub fn reverse(&mut self) { + self.memory = self.memory.iter().map(|val| !val).collect(); + } + + pub fn new(memory: Vec) -> Self { + Self { memory } + } +} + +impl Default for BasicRegister { + fn default() -> Self { + Self { + memory: vec![false], + } + } +} + +impl fmt::Display for BasicRegister { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "Memory: [")?; + + for (count, v) in self.memory.iter().enumerate() { + if count != 0 { + write!(f, ", ")?; + } + write!(f, "{}", *v as u8)?; + } + + write!(f, "]") + } +} + +fn read_vec(variable_name: &str) -> Vec { + loop { + print!("Enter {variable_name}: "); + let input: String = text_io::read!("{}\n"); + match input.chars().all(|c| c == '1' || c == '0') { + true => return input.chars().collect(), + false => eprintln!( + "[ERROR] {} may contain only 1-s and 0-s with no whitespaces!", + capitalise(variable_name) + ), + } + } +} + +pub fn char_to_bool_vector(char_vector: Vec) -> Vec { + let mut bool_vector: Vec = Vec::new(); + + for value in char_vector.iter() { + match value { + '0' => bool_vector.push(false), + '1' => bool_vector.push(true), + _ => (), + } + } + + bool_vector +} + +pub fn get_memory(variable_name: &str) -> Vec { + char_to_bool_vector(read_vec(variable_name)) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9c46024 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,44 @@ +#[allow(dead_code)] +mod bit_utilities; + +use bit_utilities::*; +// use text_io::read; + +fn main() { + let mut reg = BasicRegister::new(get_memory("your number")); + // let mut reg: BasicRegister = Default::default(); + + println!("\nRegister:"); + println!("{}", reg); + + println!("\nShifted right:"); + reg.r_shift(); + println!("{}", reg); + + println!("\nShifted left:"); + reg.l_shift(); + println!("{}", reg); + + println!("\nReversed:"); + reg.reverse(); + println!("{}", reg); + + // println!("{}", true); + // println!("{}", true); + + // let a: i8 = read!(); + // println!("{} {0:b}", a); + // println!("{} {0:b}", a >> 1); + // println!("{} {0:b}", a << 1); + // println!("{} {0:b}", !a); + // println!("{}", '3'.to_digit(2).unwrap()); + // let f_text = format!("{a:b}").chars().collect::>(); + // println!("{:?}", f_text); + // println!("{:?}", char_to_bool_vector(f_text.clone())); + // let b: BasicRegister = BasicRegister::new(char_to_bool_vector(f_text)); + // b.print_register(); + // println!("{:?}", get_memory(format!("{a:b}"))); + // println!("{:?}", a.split(" ").collect::>()); + // a = text_io::read!("{}\n"); + // println!("{a}") +}