This code here:
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
is the second
%4 necessary? wouldn't
int padding = 4 - bi.biWidth * sizeof(RGBTRIPLE) % 4;
give the same result?
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 community
Yes, it is necessary, and no, the second formula wouldn't produce the correct result.
The reason for the second %4 is to cover a corner case. If
(bi.biWidth * sizeof(RGBTRIPLE) ) %4 = 0, then 4-0 = 4. But you can't have padding of 4. Applying the second %4 does nothing to 1, 2, or 3, but will change 4 to 0. (BTW, you'll never have 0 before applying the second %4.)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)