1

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.

18
  • 1
    Bytes are 8 bits. You need to identify how many bits your number needs. Commented Dec 20, 2017 at 20:30
  • @ThorbjørnRavnAndersen I'm looking to identify how many bytes my number needs. Commented Dec 20, 2017 at 20:30
  • 2
    In Java, an int always needs 4 bytes of memory, regardless of its contents. Commented Dec 20, 2017 at 20:30
  • @Alnitak I know, but I'm looking for the exact amount of bytes the number takes up regardless. Commented Dec 20, 2017 at 20:31
  • 1
    When you've added 4 1-byte values to the buffer, how do you know if it's 4, 2 or 1 value? How can you use those values? Commented Dec 20, 2017 at 20:55

3 Answers 3

4

One line solution according to your requirements:

public int bytesCount(int n) {
    return n < 0 ? 4 : (32 - Integer.numberOfLeadingZeros(n)) / 8 + 1;
}

Where 32 - Integer.numberOfLeadingZeros(n) returns the position of the highest one-bit. After that you can easily calculate the number of required bytes.

Sign up to request clarification or add additional context in comments.

2 Comments

This was what I was looking for, as it works with signed byte values. Thank you!
You may want to check the implementation of numberOfLeadingZeros() to ensure that it is better than what you had already. grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
0

I'm sure someone has a much more efficient way to do this but a simple and easy to understand this is see what the upper bound power of 2 is higher than it, thus you now how many bits it would fit in, then you can divide by 8 to figure out how many bytes you would need.

int bytesInInt(int i) {
    int exp = 0;
    while (Math.pow(2,exp) < i) {
        exp++;
    }
    return ((exp + 1) / 8) + 1;
}

Comments

0

You can check it against the boundaries of numbers which can be stored in 1 byte (byte), 2 bytes (short), 3 bytes.

int bytes_needed(int n){
    if(n >= -128 && n <= 127){
        return 1;
    } else if(n >= -32768 && n <= 32767){
        return 2;
    } else if(n >= -8388608 && n <= 8388607){
        return 3;
    } else {
        return 4;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.