What is the difference between these two blocks of C ++ code?

Why is the first of them able to correctly increase pbf_ [k], and the second does not even do this (increase) by one time?

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]++;
 }
+5
source share
4 answers

In the first case, the masking result is converted to bool mbefore it is compared to true.

In the second case, I believe that bitmasks are an integer type. In this case true, the same integer type will be assigned (and will have a value of 1).

Just remove == truefrom the comparison to make them equivalent.

+7
source

the first checks the result of assigning m to the value bit_table_[k][i][bit_index ] & bit_mask[bit], and the second just checks

bit_table_[k][i][bit_index ] & bit_mask[bit] 0

, , m .

0

You check first

if((m=(bit_table_[k][i][bit_index ] &bit_mask[bit]))==true)

sets some value of the variable m and is taken as if.

0
source

I found one problem in your code. You need to use && instead of &. For comparison, && is a logical operator and differs from & - a bitwise operator.

Example:

if ((m = (bit_table_ [k] [i] [index bit] & mask bit [bit])) == true)

To learn about the operator in C ++, you can visit: http://www.worldbestlearningcenter.com/index_files/c++_operators.htm

0
source

All Articles