Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between these two c++ code blocks?

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]++;
 }
like image 458
John Avatar asked Dec 30 '11 09:12

John


1 Answers

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.

like image 159
Bo Persson Avatar answered Oct 28 '22 03:10

Bo Persson