且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 for 循环中迭代所有无符号整数

更新时间:2022-12-30 13:23:48

你可以用 do-while 循环来做,但是你失去了所有的细节for 语法.

You can do it with a do-while loop, but you lose all the niceties of the for syntax.

通过使用匿名块作用域,它仍然可以使用 do-while 循环:

It is still doable with do-while loop by using an anonymous block scope:

{
    unsigned i = 0;
    do { f(i); } while (++i != 0);
}

虽然这个结构可能不是最惯用的,但它显然是清晰的汇编代码的候选者.例如,gcc -O 将其编译为:

While this construct may not be most idiomatic, it is an obvious candidate for clear assembly code. For example, gcc -O compiles it as:

.L2:
        mov     edi, ebx   ; ebx starts with zero
        call    f
        add     rbx, 1
        cmp     rbx, rbp   ; rbp is set with 4294967296
        jne     .L2