if带有整数的语句 [英] if statements with integers

查看:64
本文介绍了if带有整数的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我道歉,如果这是微不足道的,一直在寻找但不能得到

找到一个很好的解释。


我常常看到如下声明:


if(val& SOME_ERROR_CODE){...

if(SOMECODE_SOMEOTHERCODE){





其中宏是整数值,通常是位。价值8,16,32



有人可以向我解释它们是真的吗?什么时候它们是假的?

什么是它背后的逻辑? (一个指向网页解释的指针

也可以做得很好)。

非常感谢。 R

Hi,
I apologize if this is trivial, been searching around but can''t get
find a good explanation.

I often see statements like

if (val&SOME_ERROR_CODE) { ...
if (SOMECODE_SOMEOTHERCODE) {

etc.

where the macros are integer values, usually "bit" values 8, 16, 32
etc.
Can someone explain to me when they are true and when they are false?
what is the logic behind it? (a pointer to a webpage explaining would
also do fine as well).
thanks very much. R

推荐答案

否*************@gmail.com 写道:

如果这是微不足道的话,我道歉,一直在寻找但是无法获得

找到一个很好的解释。


我经常看到如下声明


if(val& ; SOME_ERROR_CODE){...

if(SOMECODE_SOMEOTHERCODE){





其中宏是整数值,通常是位价值8,16,32



有人可以向我解释它们是真的吗?什么时候它们是假的?

什么是它背后的逻辑? (指向网页解释的指针

也可以。)
I apologize if this is trivial, been searching around but can''t get
find a good explanation.

I often see statements like

if (val&SOME_ERROR_CODE) { ...
if (SOMECODE_SOMEOTHERCODE) {

etc.

where the macros are integer values, usually "bit" values 8, 16, 32
etc.
Can someone explain to me when they are true and when they are false?
what is the logic behind it? (a pointer to a webpage explaining would
also do fine as well).



这是微不足道的,但不要道歉。


运营商&清除除两个操作数中设置的位之外的所有位。因此,如果其中一个操作数是单个位值,则操作结果为0

如果相应位未在其他操作数中设置,或者非零

如果相应的位*是*设置的话。如果积分表达式为0,则将整数

表达式转换为''bool''会产生''false'',如果表达式为0,则表示'b $ b''真'''不是零。因此,'if(blah& some_bit)''

检查特定位是否已设置或清除。


您给出的第二个示例是有点模糊。

''SOMECODE_SOMEOTHERCODE''是什么意思?这是单一价值吗?然后它只是

表达式在被

''if''测试之前被转换为''bool'',非零值产生''真'',零收益''假''。


V

-

请删除资金' 'A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问

It is trivial, but don''t apologise.

The operator& clears all bits except the ones set in both operands. So,
if one of the operands is a single bit value, the operation results in 0
if the corresponding bit is not set in the other operands, or non-zero
if the corresponding bit *is* set. The conversion of an integral
expression to ''bool'' yields ''false'' if the integral expression is 0, and
''true'' if the expression is not zero. Thus, the ''if (blah & some_bit)''
checks if the specific bit is set or cleared.

The second example you gave is a bit obscure. What is the meaning of
''SOMECODE_SOMEOTHERCODE''? Is that a single value? Then it''s just the
expression that gets converted to ''bool'' before being tested by the
''if'', a non-zero value yields ''true'', the zero yields ''false''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


10月9日下午2:50 *,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:
On Oct 9, 2:50*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

northsolomon ... @ gmail.com写道:
northsolomon...@gmail.com wrote:

如果这是微不足道的话我道歉,一直在寻找但是不能得到

找到一个好的解释。
I apologize if this is trivial, been searching around but can''t get
find a good explanation.


我经常看到如
I often see statements like


if(val& SOME_ERROR_CODE) ){...

if(SOMECODE_SOMEOTHERCODE){
if (val&SOME_ERROR_CODE) { ...
if (SOMECODE_SOMEOTHERCODE) {


etc.
etc.


其中宏是整数值,通常是bit价值8,16,32



有人可以向我解释它们是真的吗?什么时候它们是假的?

什么是它背后的逻辑? (指向网页解释的指针

也可以。)
where the macros are integer values, usually "bit" values 8, 16, 32
etc.
Can someone explain to me when they are true and when they are false?
what is the logic behind it? (a pointer to a webpage explaining would
also do fine as well).



这是微不足道的,但不要道歉。


运营商&清除除两个操作数中设置的位之外的所有位。 *所以,

如果其中一个操作数是单个位值,如果相应位未在其他操作数中设置,则操作结果为0

,或者非零

如果相应的位*是*设置。 *如果积分表达式为0,则将整数

表达式转换为''bool''会产生''false'',如果积分表达式为0,则会产生'b $ b''真'''表达式不是零。 *因此,'if(blah& some_bit)''

检查特定位是否已设置或清除。


您给出的第二个示例有点模糊。 *

''SOMECODE_SOMEOTHERCODE''是什么意思? *这是一个单一的价值吗? *然后它只是

表达式在被

''if''测试之前转换为''bool'',非零值收益率''真'',零收益率''假''。


V

-

请删除资金''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问 - 隐藏引用的文字 -


- 显示引用文字 -


It is trivial, but don''t apologise.

The operator& clears all bits except the ones set in both operands. *So,
if one of the operands is a single bit value, the operation results in 0
if the corresponding bit is not set in the other operands, or non-zero
if the corresponding bit *is* set. *The conversion of an integral
expression to ''bool'' yields ''false'' if the integral expression is 0, and
''true'' if the expression is not zero. *Thus, the ''if (blah & some_bit)''
checks if the specific bit is set or cleared.

The second example you gave is a bit obscure. *What is the meaning of
''SOMECODE_SOMEOTHERCODE''? *Is that a single value? *Then it''s just the
expression that gets converted to ''bool'' before being tested by the
''if'', a non-zero value yields ''true'', the zero yields ''false''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask- Hide quoted text -

- Show quoted text -



Victor,

非常感谢您的回复!


所以,如果我是正确的,那么除零之外的任何值都将在if语句中评估为TRUE

?这是否意味着如果

整数中的任何位等于1,它将变为TRUE但如果

整数中没有位等于1,则它将为FALSE ?


第二个国家包含一个拼写错误,对不起,它应该有

读取:SOMECODE || SOMEOTHERCODE但我想我明白了从

您对&的解释案件。


谢谢,R

Victor,
Thanks very much for your reply!

So if I''m correct then any value other than zero will evaluate to TRUE
in an if statement? Does it mean that if any bit is equal to 1 in an
integer it will become TRUE but if no bits are equal to 1 in the
integer it will be a FALSE?

The second statetment contained a typo, I''m sorry, it should have
read: SOMECODE||SOMEOTHERCODE but I think I understand that one from
your explanation for the "&" case.

thanks, R


没有************* @ gmail.com 写道:

[..]

所以如果我是正确的那么除零之外的任何值都会在if语句中评估为TRUE

?这是否意味着如果

整数中的任何位等于1,它将变为TRUE但如果

整数中没有位等于1,则它将为FALSE ?
[..]
So if I''m correct then any value other than zero will evaluate to TRUE
in an if statement? Does it mean that if any bit is equal to 1 in an
integer it will become TRUE but if no bits are equal to 1 in the
integer it will be a FALSE?



嗯,是的。我们不会对bool文字使用大写符号,所以,它是'b $ b''true''和''false''而不是''TRUE''和''FALSE''但是。 :-)

Well, yes. We don''t use uppercase notation for bool literals, so, it''s
''true'' and ''false'' instead of ''TRUE'' and ''FALSE'', though. :-)


第二个国家包含一个拼写错误,对不起,它应该有

读取:SOMECODE || SOMEOTHERCODE但是我想我明白那个来自

你对&的解释案件。
The second statetment contained a typo, I''m sorry, it should have
read: SOMECODE||SOMEOTHERCODE but I think I understand that one from
your explanation for the "&" case.



积分表达式与''&''和

之间存在差异,逻辑表达式与''|| '',但我会接受你的消息

了解它。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

There is a difference between an integral expression with the ''&'' and
the logical expression with the ''||'', but I will take your word that you
understand it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于if带有整数的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆