0

Is this correct way to check if bits are set in some variable and store them in another variable? Just ignore variable names, image[] and mask variables are 1 byte and first_hline_first_row and second_hline_first_row are 2 byte.

 for(i=0; i<16; i++){

    if(image[i] & mask1)         first_hline_first_row    |=   (1<<i);
    else                         first_hline_first_row    &=~  (0<<i);

    if(image[i] & mask2)         second_hline_first_row   |=   (1<<i);
    else                         second_hline_first_row   &=~  (0<<i);
    }   
Lucky
  • 199
  • 2
  • 4
  • 14

1 Answers1

0

Two things:

  • image[i] & mask1 only checks whether image[i] and mask1 have at least one bit in common. Is this your intention?
  • first_hline_first_row &=~ (0<<i); does nothing (~(0<<i) is all ones).

From the comments I'm guessing that this is what you meant to write:

for(i=0; i<16; i++){
     if(image[i] & mask1)
     {
         first_hline_first_row    |=   (1<<i);
     }
     else                         
     {
         first_hline_first_row    &=~  (1<<i);
     }

     if(image[i] & mask2)
     {
         second_hline_first_row   |=   (1<<i);
     }
     else
     {
         second_hline_first_row   &=~  (1<<i);
     } 
}
banach-space
  • 1,781
  • 1
  • 12
  • 27