I’m porting some c code which is doing a modulo on an uint32_t. An uint32_t fits in a Java int bit-wise, but I cannot figure out how to perform a modulo operation on it without converting to a long. This is the code I have right now:
int i = 0xffffffff; long asUnsigned = Integer.toUnsignedLong(i); int mod = (int) (asUnsigned % 42L);
Can I perform this modulo calculation without converting to long?
Advertisement
Answer
Use Integer.remainderUnsigned(int dividend, int divisor)
(javadoc):
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.