Help on the verbosity of Shifts

It’s too verbose to use “<<” or “>>”, from the perspective of a C programmer. :grinning:

How to make it simpler?

fn run_items(bitmap: u16, item_cnt: c_int, items: [*c] const run_item_t) bool {
        var index: usize = 0;
        while (index < item_cnt) : (index += 1) {
            if ((bitmap & (@as(u16, 1) << @intCast(u4, index))) == 0) continue;
            // ...
        }
        return true;
    }
1 Like

one thing i will note is that @intCast(u4, index) where index is a usize bounded by a c_int is incredibly unsafe and will crash after 16 loop iterations

1 Like

Looks like you could have good use of the @ctz builtin. Instead of looping through item_cnt bits of your bitmap, you could just loop over the 1 bits.

fn run_items(bitmap: u16, item_cnt: c_int, items: [*c] const run_item_t) bool {
    var index: usize = @ctz(bitmap);
    while (index < item_cnt) : (index += 1 + @ctz(bitmap >> (index + 1))) {
        // ...
    }
    return true;
}

I’m not even a zig newbie, this is my first zig code ever written and I don’t have a zig environment so I can’t tell whether it compiles. This is from reading the docs only.

Interesting.

But I think this is harder to understand than the original one. And, @ctz seems not available on all processors. (Oh, if available, it’s faster than the original one, :slight_smile: )