Why is Math.floor() used instead of integer division in BER Codec

encoding, java, snmp

Solution

To answer the technical part of your question:

Using `math.floor()` is superfluous: `temp / 256` is an integer (by Java's rules for integer arithmetic), and using `Math.floor()` on an integer is pointless. You could simply use `temp / 256`.

Why the author did this is impossible to answer without reading their mind. The author might simply have been confused about the behaviour of division in Java, and decided to "play it safe" - but that is just speculation.

Problem

I am looking at the SNMPBEECodec which can be seen at this location In particular I am looking at the function `encodeLength()` A snippet I am interested in ``` int numBytes = 0; int temp = length; while (temp > 0) { ++numBytes; temp = (int)Math.floor(temp / 256); } ``` (from the Drexel SNMP library). I would like to know why `Math.floor()` is used instead of just a simple integer division like `temp/256`. It seems the simple integer division would give the same result. Or is there a technical difference?

Original source

Related problems