Dynamic "for"

You need to use the reversed indices:

function test3(v::Val{N}) where {N}
    p2 = reverse(ntuple(i->2^(i-1), v))
    for idx in CartesianIndices(ntuple(i->0:1, v))
        ridx = reverse(Tuple(idx))
        n = sum(ridx .* p2)
        println(ridx[1], ridx[2], ridx[3], " => ", n)
    end
end
000 => 0
001 => 1
010 => 2
011 => 3
100 => 4
101 => 5
110 => 6
111 => 7
2 Likes