rust/example/std_example.rs
2019-07-26 11:30:39 +02:00

37 lines
1.3 KiB
Rust

#![feature(core_intrinsics)]
use std::io::Write;
use std::intrinsics;
fn checked_div_i128(lhs: i128, rhs: i128) -> Option<i128> {
if rhs == 0 || (lhs == -170141183460469231731687303715884105728 && rhs == -1) {
None
} else {
Some(unsafe { intrinsics::unchecked_div(lhs, rhs) })
}
}
fn checked_div_u128(lhs: u128, rhs: u128) -> Option<u128> {
match rhs {
0 => None,
rhs => Some(unsafe { intrinsics::unchecked_div(lhs, rhs) })
}
}
fn main() {
assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26);
assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7);
checked_div_i128(0i128, 2i128);
checked_div_u128(0u128, 2u128);
assert_eq!(1u128 + 2, 3);
// overflow panic
// 0xFEDCBA987654321123456789ABCDEFu128 + 0xFEDCBA987654321123456789ABCDEFu128;
println!("{}", 0b100010000000000000000000000000000u128 >> 10);
println!("{}", 0xFEDCBA987654321123456789ABCDEFu128 >> 64);
println!("{} >> 64 == {}", 0xFEDCBA987654321123456789ABCDEFu128 as i128, 0xFEDCBA987654321123456789ABCDEFu128 as i128 >> 64);
println!("{}", 353985398u128 * 932490u128);
}