jogabonito - 4 months ago 28

Java Question

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()`

`temp/256`

Answer

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.