This is likely to be a duplicate question, but I was unable to find a similar one when searching.
I'm looking for an easy, efficient method to determine how many signed bytes an int requires at runtime.
For example, consider an int with the following values:
1 - Requires 1 Byte
10 - Requires 1 Byte
128 - Requires 2 Bytes
1024 - Requires 2 Bytes
32768 - Requires 3 Bytes
...
Integer.MAX_VALUE - Requires 4 Bytes
Edit: It's obvious to me that an int requires 4 bytes of memory regardless of its value. Nevertheless, I'm looking for the amount of bytes that the value would take up if that wasn't the case.
Ideally the answer I'm looking for utilizes bit manipulation and returns a value of 1 for an input of 0.
bytes my number needs.intalways needs 4 bytes of memory, regardless of its contents.