0

Line 65 in copy.c

int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

Question is why do we need last modulo operator in bold? Even without it, it gives the right answer.

1 Answer 1

1

It's not needed for a padding of 1, 2, or 3 bytes.

But imagine what happened if bi.biWidth were a multiple of 4.

bi.biWidth * sizeof(RGBTRIPLE) would be a multiple of 4.

(bi.biWidth * sizeof(RGBTRIPLE)) % 4 would be 0.

4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4 would be 4, but should be 0. You'd fix that by applying another %4.

2
  • Ok,I was applying parenthesis in a different way. In that case, we dont need last %4 to get right padding
    – rasdocus
    Mar 14, 2017 at 19:28
  • {4 - [bi.biWidth * sizeof(RGBTRIPLE)]} % 4
    – rasdocus
    Mar 14, 2017 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .