I need a function in Java to give me number of bytes needed to represent a given integer. When I pass 2 it should return 1, 400 -> 2, 822222 -> 3, etc.
@Edit: For now I’m stuck with this:
numOfBytes = Integer.highestOneBit(integer) / 8
Don’t know exactly what highestOneBit() does, but have also tried this:
numOfBytes = (int) (Math.floor(Math.log(integer)) + 1);
Which I found on some website.
1 Answer