Why is (a % 256) different than (a & 0xFF)?

It’s not the same. Try num = -79, and you will get different results from both operations. (-79) % 256 = -79, while (-79) & 0xff is some positive number.

Using unsigned int, the operations are the same, and the code will likely be the same.

PS- Someone commented

They shouldn’t be the same, a % b is defined as a - b * floor (a / b).

That’s not how it is defined in C, C++, Objective-C (ie all the languages where the code in the question would compile).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)