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.
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityIt'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
.