我在阅读 Stack Overflow 问题后遇到了这个问题 打印 int使用 C 进行二进制表示。
在用户的评论中,他们发布了这个 for
循环,该循环将 1 或 0 分配给位位置,以便从 int 十进制转换为 char * 二进制。
for(; bits--; u >>= 1)
str[bits] = u & 1 ? '1' : '0';
我明白为什么不需要初始化值。这是我一直都知道的 for
循环的语法:
for ( variable initialization; condition; variable update )
我不明白“bit--”如何成为退出条件。请帮助我理解这段代码是如何工作的(我测试过它,它是有效的)。
I had this question after reading the Stack Overflow quesion Print an int in binary representation using C.
In a user's comment, they posted this for
loop, which assigns either a 1 or a 0 to the bit position in order to convert from an int decimal to char * binary.
for(; bits--; u >>= 1)
str[bits] = u & 1 ? '1' : '0';
I understand why there doesn't need to be an initialized value. This is the syntax for a for
loop that I've always known:
for ( variable initialization; condition; variable update )
I don't understand how 'bit--' can be an exit condition. Please help me understand how this code works (I tested it, and it is valid).
发布评论
评论(4)
在 C 语言中,零值在布尔上下文中计算为“假”。因此,当
bits--
计算结果为0
时,在循环上下文中,它的计算结果为“false”并终止循环。例如,如果您说:
它将输出“False”,因为
--x
计算结果为 0,这在布尔上下文中是“false”。In C, a value of zero evaluates to "false" in a Boolean context. So when
bits--
evaluates to0
, in the context of the loop it evaluates to "false" and terminates the loop.If you say, for example:
It will output "False", because
--x
evaluates to 0, which is "false" in a Boolean context.所有条件基本上都可以归结为检查某个值是否为 0。 0 表示
假
,其他一切表示真
。因此,当bits
为 0 时,循环就会中断。有时您会看到
while
或if
条件的写法,这只是
So
的简写
All conditions basically boil down to checking whether something is 0 or not. 0 means
false
, everything else meanstrue
. So that loop will break whenbits
is 0.You will sometimes see
while
orif
conditions writtenThat is just shorthand for
So
is short for
bits-- 是 int 类型的赋值表达式(因为它将返回 b 的值,即 int)。为了匹配for循环语法,它被转换为布尔表达式,这意味着如果bits != 0则为真。
事实上,条件与bits!=0相同,但是通过使用bits --,同时改变了位的值,使代码更加紧凑。就这样。
bits-- is an assignment expression of type int (since it will return the value of b, which is int). To match the for loop syntax, it gets converted to a Boolean expression, which means it is true if bits != 0.
In fact, the condition is identical to bits!=0, but by using bits--, it changes the value of bits at the same time, making the code more compact. That's all.
正如其他人所说,在 C 中,您可以使用整数作为条件 - 0 或
false
,以及任何其他true
。 (实际上,您几乎总是这样做 - 即使像a 这样的表达式也是一个 int。)
循环将结束。
因此,当
bits--
为 0时,--
运算符位于变量之后,它减少变量并获取其先前的值。例如,如果您有 int a=3,b; b=a--;,则 b 将为 3,a 将为 2。因此,当该位从
0
减少到-1 后,循环将退出
。这意味着,如果在开始时,
bits==8
(例如),则循环将迭代 8 次,而在第一次时,bits 将为 7(因为条件已检查),并且最后,位将为 0。这是循环数组的好方法(因为在 C 中,bits
变量数组的索引是从0
到 <代码>位-1)。As others said, in C, you can use integers as a condition - 0 or
false
, and anything else fortrue
. (Actually, you almost always do it - even an expression likea<b
is an int.)So, the loop will end when
bits--
will be 0.When the
--
operator comes after the variable, it decreases the variable, and gets the previous value of it. For example, if you haveint a=3,b; b=a--;
, then b will be 3, and a will be 2.So, the loop will exit after that bits will been decreased from
0
to-1
.That means that, if in the beginning,
bits==8
(for example), the loop will iterate 8 times, when in the first, bits will be 7 (because the condition had checked), and in the last, bits will be 0. It is a nice way to loop through an array (Since in C, an array ofbits
variables is being indexed from0
tobits-1
).