about a = b | c | ...
-
a = b | c | ... (1) if (a & b) { .... } (2) if ((a & b) == b) { .... } (1) and (2), use which is better? why? thanks.
If the value '
b
' is a single bit, then the two approaches are equivalent. If there are multiple bits set in 'b
', then they are not. (1) will be true if any of the bits inb
are also set ina
. (2) will be true if and only if the same bits that are set inb
are also set ina
.
Software Zen:
delete this;
-
If the value '
b
' is a single bit, then the two approaches are equivalent. If there are multiple bits set in 'b
', then they are not. (1) will be true if any of the bits inb
are also set ina
. (2) will be true if and only if the same bits that are set inb
are also set ina
.
Software Zen:
delete this;
-
a = b | c | ... (1) if (a & b) { .... } (2) if ((a & b) == b) { .... } (1) and (2), use which is better? why? thanks.
It depends. If each of
b,c,...
corrensponds to just one bit set (e.g. b=1; c=2;...), then the expressions are equivalent (maybe (1) is faster). On the other hand, ifb,c,...
holds composite bit values (e.g. b=3; c=5;...) then you have to carefully choose the expression you need, since (1) meets the requisite of an OR condition, while (2) corrensponds to an AND one). For instance, conside the following code snippet:enum
{
FEMALE = 1, //MALE: bit unset
YOUNG = 2, //OLD: bit unset
...
};if
b=FEMALE;
then both the (1) and (2) meet the condition, but if you are searching for a young female (b=YOUNG|FEMALE;
), the you have to be careful, because (1) holds true also whena
represents a young guy or an old lady! :):-D:)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
It depends. If each of
b,c,...
corrensponds to just one bit set (e.g. b=1; c=2;...), then the expressions are equivalent (maybe (1) is faster). On the other hand, ifb,c,...
holds composite bit values (e.g. b=3; c=5;...) then you have to carefully choose the expression you need, since (1) meets the requisite of an OR condition, while (2) corrensponds to an AND one). For instance, conside the following code snippet:enum
{
FEMALE = 1, //MALE: bit unset
YOUNG = 2, //OLD: bit unset
...
};if
b=FEMALE;
then both the (1) and (2) meet the condition, but if you are searching for a young female (b=YOUNG|FEMALE;
), the you have to be careful, because (1) holds true also whena
represents a young guy or an old lady! :):-D:)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Example 1: Suppose
a=0x1A
(00011010
binary), andb=0x08
(00001000
). In that case, 'if (a & b)
' and 'if ((a & b) == b)
' are both satisfied, since 'a & b
' evaluates to0x08
. Example 2: Supposea=0x1A
(00011010
binary) again, but this timeb=0x09
(00001001
binary). In that case, 'if (a & b)
' is satisfied, yet 'if ((a & b) == b)
' is not. Remember that the firstif
is satisfied by any non-zero value, while the second one requires that the result of the '(a & b)
' expression equal the value ofb
.
Software Zen:
delete this;
-
:confused: a = 1 | 0x10000000 a & 3 = 1 -> if (a & 3) { ...} error a & 4 = 0 not understand about bit a = b | c | d how to define value of b,c, d
What I mean is that if you have
b,c,d,e,f,...
each corrensponding to only one bit set, for instance:b=0x00000001;// bit 0 set
c=0x00000002;// bit 1 set
d=0x00000004;// bit 2 set
e=0x00000008;// bit 3 set
f=0x00000010;// bit 4 setthen a condition using
(a & b)
evaluates to true (non-zero) only ifa
has the bit 0 set and the same holds for a condition using((a & b) == b)
. In the same way, a condition using(a & c)
evaluates to non-zero only ifa
has the bit 1 set, the same holding for a condition using((a & c)==c)
, and so on... On the other hand, suppose thatx
is a combination (sum using OR) of the above indipendent values, for instancex= b | e;
, so thatx=0x00000009
, i.e.x
has both the bits 0 and 3 set. In this case, a condition using(a & x)
evaluates to non-zero either ifa
has:* only bit 0 set- only bit 3 set
- both bit 0 and 3while a condition using
((a & x) == x)
holds true only if both bit 0 and 3 are set. In other words, ifx = 0x00000009;
then(a & x)
holds true ifa = 0x00000001;
, ora = 0x00000008;
, ora = 0x00000009;
while((a & x) == x)
holds true ifa = 0x00000009
. hope that helps :) BTW both conditions are also true ifa = 0x00000019
and so on...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.