Reputation: 804
Why is the first one able to increment pbf_[k] correctly while the second one does not even do it(increment)for once?
unsigned pbf_[5] ={0};
bool m=0;
Code 1:
for(int k=0;k<5;k++)
{
if((m=(bit_table_[k][i][bit_index ] &bit_mask[bit]))==true)
pbf_[k]++;
}
Code 2:
for(int k=0;k<5;k++)
{
if((bit_table_[k][i][bit_index ] & bit_mask[bit])==true)
pbf_[k]++;
}
Upvotes: 5
Views: 214
Reputation: 11
I found one problem in your code. You need to use && instead of &. In comparison, && is an logical operator and it differs from &--Bitwise operator.
Example:
if((m=(bit_table_[k][i][bit_index ] && bit_mask[bit]))==true)
To learn about operator in C++ you can visit:http://www.worldbestlearningcenter.com/index_files/c++_operators.htm
Upvotes: 0
Reputation: 92261
In the first case, the result of the masking is converted to bool m
before it is compared to true.
In the second case, I believe the bitmasks are some integer type. In that case true
will be promoted to the same integer type (and have the value 1).
Just remove the == true
from the comparison to make them equivalent.
Upvotes: 7
Reputation: 278
You first check
if((m=(bit_table_[k][i][bit_index ] &bit_mask[bit]))==true)
is assining some value to variable m and that is taken by if as true.
Upvotes: 0
Reputation: 65496
the first on tests the result of the the assignment to m of the value of bit_table_[k][i][bit_index ] & bit_mask[bit]
, while the second just tests whether
bit_table_[k][i][bit_index ] & bit_mask[bit]
results not 0
Both the same effect, except that the first records the result in m on each iteration.
Upvotes: 0