Skip to content

Instantly share code, notes, and snippets.

@lbiaggi
Created January 18, 2022 19:41
Show Gist options
  • Save lbiaggi/3f2b3db4376deed286e7c3f08ab59e30 to your computer and use it in GitHub Desktop.
Save lbiaggi/3f2b3db4376deed286e7c3f08ab59e30 to your computer and use it in GitHub Desktop.
Calculator with support for overflow/underflow
use std::env;
fn sub(a: u64, b:u64) -> u64 {
a.wrapping_sub(b)
}
fn add(a:u64, b:u64) -> u64 {
a.wrapping_add(b)
}
fn main() {
let args: Vec<String> = env::args().collect();
let result: u64 = if &args[3] == "add" {
add(
u64::from_str_radix(&args[1].trim_start_matches("0x"),16).unwrap(),
u64::from_str_radix(&args[2].trim_start_matches("0x"),16).unwrap(),
)}
else {
sub(
u64::from_str_radix(&args[1].trim_start_matches("0x"),16).unwrap(),
u64::from_str_radix(&args[2].trim_start_matches("0x"),16).unwrap(),
)};
println!("0x{:x}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment