Nimber arithmetic: Difference between revisions
m
Rust - reformatted with rustfmt
Thundergnat (talk | contribs) m (→{{header|Raku}}: Twiddles; also demo operations on > 64 bit numbers) |
m (Rust - reformatted with rustfmt) |
||
Line 813:
{{trans|FreeBASIC}}
<lang rust>// highest power of 2 that divides a given number
fn hpo2(n
n & (0xFFFFFFFF - n + 1)
}
// base 2 logarithm of the highest power of 2 dividing a given number
fn lhpo2(n
let mut q
let mut m
while m % 2 == 0 {
m >>= 1;
Line 829:
// nim-sum of two numbers
fn nimsum(x
x ^ y
}
// nim-product of two numbers
fn nimprod(x
if x < 2 || y < 2 {
return x * y;
}
let mut h
if x > h {
return nimprod(h, y) ^ nimprod(x ^ h, y);
Line 845:
return nimprod(y, x);
}
let xp
let yp
let comp
if comp == 0 {
return x * y;
Line 855:
}
fn print_table(n
print!(" {} |", op);
for a in 0..=n {
Line 878:
println!();
print_table(15, '*', nimprod);
let a
let b
println!("\n{} + {} = {}", a, b, nimsum(a, b));
println!("{} * {} = {}", a, b, nimprod(a, b));
|