sample-code: Expand the capabilities of the sample code

This commit is contained in:
2024-07-11 04:02:44 -05:00
parent a646a9e521
commit fffc370380
2 changed files with 65 additions and 10 deletions

View File

@@ -1,12 +1,31 @@
pub fn hex(mut n: u64) {
let hex_lut = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];
for xd in 0..16 {
let n = n >> (15 - xd) * 4;
if n != 0 {
print(hex_lut[n & 0xf])
}
//! Formats numbers in hexadecimal, octal, or binary
mod math;
// TODO: casting and/or conversion
const HEX_LUT: Array = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];
pub fn hex(n: u64) {
let out = "0x";
for xd in min(count_leading_zeroes(n) / 4, 15)..16 {
out += HEX_LUT[(n >> (15 - xd) * 4) & 0xf]
}
println()
out
}
pub fn oct(n: u64) {
let out = "0o";
for xd in min((count_leading_zeroes(n) + 2) / 3, 21)..22 {
out += HEX_LUT[(n >> max(63 - (3 * xd), 0)) & 7]
}
out
}
pub fn bin(n: u64) {
let out = "0b";
for xd in min(count_leading_zeroes(n), 63)..64 {
out += HEX_LUT[(n >> 63 - xd) & 1]
}
out
}