feat(ex00|ex01|ex02): adder, multiplier and gray code
This commit is contained in:
parent
ca5f670ac3
commit
8031391a61
|
@ -10,3 +10,8 @@ Cargo.lock
|
|||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "ready_set_boole"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,15 @@
|
|||
mod tests;
|
||||
|
||||
pub fn adder(a: u32, b: u32) -> u32 {
|
||||
let mut result: u32 = 0;
|
||||
let mut carriage: u32 = 0;
|
||||
|
||||
for i in 0..32 {
|
||||
let a_bit = a >> i & 1;
|
||||
let b_bit = b >> i & 1;
|
||||
result = result | (a_bit ^ b_bit ^ carriage) << i;
|
||||
carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage)
|
||||
}
|
||||
result
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::adder::adder;
|
||||
use std::num::Wrapping;
|
||||
|
||||
#[test]
|
||||
fn zero_plus_one() {
|
||||
assert_eq!(adder(0, 1), 0 + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn four_plus_three() {
|
||||
assert_eq!(adder(4, 3), 4 + 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_plus_one() {
|
||||
assert_eq!(adder(1, 1), 1 + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_numbers() {
|
||||
assert_eq!(adder(632_235, 12_756), 632_235 + 12_756);
|
||||
assert_eq!(adder(932_455, 654_756), 932_455 + 654_756);
|
||||
assert_eq!(adder(332_875, 123_655), 332_875 + 123_655);
|
||||
assert_eq!(adder(732_235_342, 234_756), 732_235_342 + 234_756);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow() {
|
||||
let big = Wrapping(3_245_532_214u32);
|
||||
assert_eq!(adder(3_245_532_214, 3_245_532_214), (big + big).0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
mod tests;
|
||||
|
||||
pub fn gray_code(n: u32) -> u32 {
|
||||
let mut result: u32 = n & 1 << 31;
|
||||
|
||||
for i in (1..32).rev() {
|
||||
let n_bit = n >> i & 1;
|
||||
let n_right_bit = n << 1 >> i & 1;
|
||||
result = result | (n_bit ^ n_right_bit) << i >> 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::gray_code::gray_code;
|
||||
|
||||
#[test]
|
||||
fn digits() {
|
||||
assert_eq!(gray_code(0), 0);
|
||||
assert_eq!(gray_code(1), 1);
|
||||
assert_eq!(gray_code(2), 3);
|
||||
assert_eq!(gray_code(3), 2);
|
||||
assert_eq!(gray_code(4), 6);
|
||||
assert_eq!(gray_code(5), 7);
|
||||
assert_eq!(gray_code(6), 5);
|
||||
assert_eq!(gray_code(7), 4);
|
||||
assert_eq!(gray_code(8), 12);
|
||||
assert_eq!(gray_code(9), 13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn msb_u32() {
|
||||
assert_eq!(gray_code(3_000_000_000), 3_954_733_312);
|
||||
assert_eq!(gray_code(2_147_483_648), 3_221_225_472);
|
||||
assert_eq!(gray_code(4_294_967_295), 2_147_483_648);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
mod adder;
|
||||
mod multiplier;
|
||||
mod gray_code;
|
||||
|
||||
use gray_code::gray_code;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
println!("{}", gray_code(4_294_967_295));
|
||||
println!("{}", gray_code(511));
|
||||
println!("{}", gray_code(255));
|
||||
println!("{}", gray_code(127));
|
||||
println!("{}", gray_code(63));
|
||||
println!("{}", gray_code(31));
|
||||
println!("{}", gray_code(15));
|
||||
println!("{}", gray_code(7));
|
||||
println!("{}", gray_code(3));
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
mod tests;
|
||||
|
||||
use crate::adder::adder;
|
||||
|
||||
pub fn multiplier(a: u32, b: u32) -> u32 {
|
||||
let mut result = 0;
|
||||
|
||||
for i in 0..32 {
|
||||
if a >> i & 1 == 1 {
|
||||
result = adder(result, b << i);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::multiplier::multiplier;
|
||||
use std::num::Wrapping;
|
||||
|
||||
#[test]
|
||||
fn zero_times_one() {
|
||||
assert_eq!(multiplier(0, 1), 0 * 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn four_times_three() {
|
||||
assert_eq!(multiplier(4, 3), 4 * 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_times_one() {
|
||||
assert_eq!(multiplier(1, 1), 1 * 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_numbers() {
|
||||
assert_eq!(multiplier(62_235, 12_756), 62_235 * 12_756);
|
||||
assert_eq!(multiplier(93_455, 4_756), 93_455 * 4_756);
|
||||
assert_eq!(multiplier(32_875, 23_655), 32_875 * 23_655);
|
||||
assert_eq!(multiplier(732_342, 796), 732_342 * 796);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow() {
|
||||
let big = Wrapping(3_245_532_214u32);
|
||||
assert_eq!(multiplier(3_245_532_214, 3_245_532_214), (big * big).0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue