两个作业之间有什么区别 [英] what is the difference between two assignments
问题描述
header = hdr_type& 0x80;并且
header = !!(hdr_type& 0x80);
我在某些地方见过第二种作业。是吧有什么特别原因吗?
问候
rupesh
Hi,
Is there any difference between
header = hdr_type & 0x80 ; and
header = !!(hdr_type & 0x80);
I have seen the second kind of assignment in some places.Does it have
any special reason ?
Regards
rupesh
推荐答案
>
header = hdr_type& 0x80;和/ /
header = !!(hdr_type& 0x80);
是的。对于第一个,标题可能具有值0或0x80。
对于第二个,标题可能具有值0或1.
有时人们喜欢有布尔的值(无论
它存储的类型)仅限于值0或1.
我在某些地方看到了第二种类型的赋值。它有什么特别的原因吗?
header = hdr_type & 0x80 ; and
header = !!(hdr_type & 0x80);
Yes. For the first one, header might have the values 0 or 0x80.
For the second one, header might have the values 0 or 1.
Sometimes people prefer to have "boolean" values (regardless of
what type it''s stored in) limited to the values 0 or 1.
I have seen the second kind of assignment in some places.Does it have
any special reason ?
根据立法,原因并不符合特殊要求
控制情报机构(矛盾! )在美国。
Gordon L. Burditt
The reason does not qualify as special under the legislation
controlling intelligence agencies (oxymoron!) in the United States.
Gordon L. Burditt
ru ******** @ rediffmail.com 写道:
是否有任何区别
header = hdr_type& 0x80;并且
header = !!(hdr_type& 0x80);
我在某些地方见过第二种任务。它有什么特殊原因吗? ?
问候
rupesh
Hi,
Is there any difference between
header = hdr_type & 0x80 ; and
header = !!(hdr_type & 0x80);
I have seen the second kind of assignment in some places.Does it have
any special reason ?
Regards
rupesh
结果是0或1而不是0或0x80,
也许是因为对结果的渴望是像C99 _Bool那样的b $ b。
-David
It is making the result 0 or 1 instead of 0 or 0x80,
perhaps because of some desire for the result to be
like a C99 _Bool.
-David
ru********@rediffmail.com 写道:
header = hdr_type& 0x80;并且
这使得标题为0或0x80。
header = !!(hdr_type& 0x80);
这使得标题为0或者1,因为''!''产生0或1.这可能会让人感到困惑,因为它忽略了一个众所周知的布尔逻辑定律:
!! a =一个;双重否定没有效果,可以删除。因为C'的布尔值概念不是
双值类型,所以这里没有
的工作方式,所以!!确实有效并且实际上转换了它对一位值的
参数。它作为布尔值的值仍然没有改变,
虽然:对于布尔测试,无论你是在1还是0x80上测试都没关系。
我见过第二个在某些地方进行任务。是否有任何特殊原因?
Hi,
Is there any difference between
header = hdr_type & 0x80 ; and
This makes header either 0 or 0x80.
header = !!(hdr_type & 0x80);
This makes header either 0 or 1, since ''!'' yields either 0 or 1. This
can be confusing because it ignores a well-known law of boolean logic:
!!a = a; double negation has no effect and can be removed. It doesn''t
work that way here because C''s notion of a boolean is not that of a
two-value type, so !! does have effect and actually "converts" its
argument to a one-bit value. Its value as a boolean is still unchanged,
though: for a boolean test it doesn''t matter whether you test on 1 or 0x80.
I have seen the second kind of assignment in some places.Does it have
any special reason ?
是的,这是(在我看来)不太直观的写作方式>
header = hdr_type& 0x80? 1:0;
当然,如果你只使用''header''作为布尔值,在测试中,没有
需要这个第一个地方你可以使用''hdr_type& 0x80''。
S.
Yes, it''s an (in my opinion) less intuitive way of writing
header = hdr_type & 0x80 ? 1 : 0;
Of course, if you only use ''header'' as a boolean, in tests, there is no
need for this in the first place and you can just use ''hdr_type & 0x80''.
S.
这篇关于两个作业之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!