Last active
September 3, 2021 09:09
-
-
Save matu3ba/eeefc22bb5f93ef7615a8497ff8e0191 to your computer and use it in GitHub Desktop.
helper program to cross check correctness of ffs with simpler algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const maxInt = std.math.maxInt; | |
const print = std.debug.print; | |
test "print ffs" { | |
//var in: u64 = 0; | |
//var in: u32 = 256; | |
//var in: u64 = 256; | |
var in: u128 = 256; | |
var x: @TypeOf(in) = 0; | |
var out: @TypeOf(in) = 0; | |
print("\n", .{}); | |
//while (in <= 0xff) : (in += 1) { | |
while (in < maxInt(@TypeOf(in))) : (in = @mulWithSaturation(in, 2)) { | |
x = in; // must use temporary for correct iteration | |
out = 0; | |
// ffs - find first set | |
var index: u32 = 0; | |
if (x != 0) { | |
while (((~x) & 1) != 0) { | |
x = x >> 1; | |
index += 1; | |
} | |
} | |
//const one: @TypeOf(in) = 1; | |
//out = one << index; | |
out = index; | |
//print("{d:>40}", .{in}); | |
print(" {x:>35}", .{in}); | |
//print(" {b:>32}", .{in}); | |
print(" {d:>5}\n", .{out}); //result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment