猿问

返回存储长值所需的字节数的函数

我创建了一个函数,该函数返回存储长值所需的字节数。该值必须在[Long.MIN_VALUE, Long.MAX_VALUE].


private static final int ONE_BYTE_MAX = (1 << 7) - 1; // 127

ONE_BYTE_MAX值为127


private static final int ONE_BYTE_MIN = -(1 << 7);  // -128

ONE_BYTE_MIN值为-128


private static final int TWO_BYTE_MAX = (1 << 15) - 1; 


private static final int TWO_BYTE_MIN = -(1 << 15);


private static final int THREE_BYTE_MAX = (1 << 23) - 1; 


private static final int THREE_BYTE_MIN = -(1 << 23);


private static final long FOUR_BYTE_MAX = (1L << 31) - 1;


private static final long FOUR_BYTE_MIN = -(1L << 31);


private static final long FIVE_BYTE_MAX = (1L << 39) - 1; 


private static final long FIVE_BYTE_MIN = -(1L << 39);


private static final long SIX_BYTE_MAX = (1L << 47) - 1; 


private static final long SIX_BYTE_MIN = -(1L << 47);


private static final long SEVEN_BYTE_MAX = (1L << 55) - 1; 


private static final long SEVEN_BYTE_MIN = -(1L << 55);

方法


public static int getBytesForLongValue(long value) {

        if (value >= ONE_BYTE_MIN && value <= ONE_BYTE_MAX) {

            return 1;

        } else if (value >= TWO_BYTE_MIN && value <= TWO_BYTE_MAX) {

            return 2;

        } else if (value >= THREE_BYTE_MIN && value <= THREE_BYTE_MAX) {

            return 3;

        } else if (value >= FOUR_BYTE_MIN && value <= FOUR_BYTE_MAX) {

            return 4;

        }else if (value >= FIVE_BYTE_MIN && value <= FIVE_BYTE_MAX) {

            return 5;

        }else if (value >= SIX_BYTE_MIN && value <= SIX_BYTE_MAX) {

            return 6;

        }else if (value >= SEVEN_BYTE_MIN && value <= SEVEN_BYTE_MAX) {

            return 7;

        } else {

            return 8;

        }

    }  

有没有一种简单的方法可以在Java中做


繁花不似锦
浏览 160回答 3
3回答

jeck猫

尽管我认为对于Java中的长原始类型,我们应该将字节数保留为8,但是还有另一种方法来计算保留长值(包括符号位)的最小字节数。此方法也可用于计算任何整数,以调整该方法的签名的大小:public static int getMinNoOfBytes(BigInteger value)不过,这里是代码:public class Answer {&nbsp; &nbsp; public static int getMinNoOfBytes(long value) {&nbsp; &nbsp; &nbsp; &nbsp; BigInteger any = BigInteger.valueOf(value);&nbsp; &nbsp; &nbsp; &nbsp; return any.toByteArray().length;&nbsp; &nbsp; }&nbsp; &nbsp; //Test&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; //Long.MAX_VALYE&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(getMinNoOfBytes(Long.MAX_VALUE));&nbsp; &nbsp; &nbsp; &nbsp; //Long.MIN_VALUE&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(getMinNoOfBytes(Long.MIN_VALUE));&nbsp; &nbsp; &nbsp; &nbsp; //Any value&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(getMinNoOfBytes(65536));&nbsp; &nbsp; }}输出是(按预期)8 8 3

繁星coding

您可以使用两个字段创建一个枚举:最小值和最大值。然后遍历该枚举值,并在循环内只有一个if语句:public enum Range {&nbsp; &nbsp; ONE&nbsp; (1, -(1L << 7 ), (1L << 7 ) - 1),&nbsp; &nbsp; TWO&nbsp; (2, -(1L << 15), (1L << 15) - 1),&nbsp; &nbsp; THREE(3, -(1L << 23), (1L << 23) - 1),&nbsp; &nbsp; FOUR (4, -(1L << 31), (1L << 31) - 1),&nbsp; &nbsp; FIVE (5, -(1L << 39), (1L << 39) - 1),&nbsp; &nbsp; SIX&nbsp; (6, -(1L << 47), (1L << 47) - 1),&nbsp; &nbsp; SEVEN(7, -(1L << 55), (1L << 55) - 1);&nbsp; &nbsp; public final int bytesNeeded;&nbsp; &nbsp; public final long min;&nbsp; &nbsp; public final long max;&nbsp; &nbsp; Range(int bytesNeeded, long min, long max) {&nbsp; &nbsp; &nbsp; &nbsp; this.bytesNeeded = bytesNeeded;&nbsp; &nbsp; &nbsp; &nbsp; this.min = min;&nbsp; &nbsp; &nbsp; &nbsp; this.max = max;&nbsp; &nbsp; }&nbsp; &nbsp; public static int getNbBytesForLongValue(long value) {&nbsp; &nbsp; &nbsp; &nbsp; for (Range range : Range.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (range.min <= value && value <= range.max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return range.bytesNeeded;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return 8;&nbsp; &nbsp; }}编辑:查看这些枚举定义,实际上很容易将其完全转换为循环:public static int getNbBytesForLongValue(long value) {&nbsp; &nbsp; for (int i = 0; i < 7; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (-(1L << (7 + i * 8)) <= value && value <= (1L << (7 + i * 8)) - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i + 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return 8;}

收到一只叮咚

在这个答案中,我们关注所需的位数。从那里开始,可以通过将ceil除以8来计算字节数。int bytesNeeded(long number) {&nbsp; &nbsp; return (bitsNeeded(number) + 7) / 8;}我假设我们将所有数字存储在二进制补码中,因此我们需要5而不是4位来存储数字8 dec = 0 1000 bin,依此类推。对于适合一个数字 longint bitsNeeded(long number) {&nbsp; &nbsp; if (number < 0) {&nbsp; &nbsp; &nbsp; &nbsp; number = ~number; // same as -(number + 1)&nbsp; &nbsp; }&nbsp; &nbsp; return Long.SIZE - Long.numberOfLeadingZeros(number) + 1;}结果&nbsp;i | bitsNeeded(i) | binary representation of i in two's complement---+---------------+------------------------------------------------8 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 1000-7 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 1001-6 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 1010-5 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 1011-4 |&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp;100-3 |&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp;101-2 |&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 10-1 |&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp;1&nbsp;0 |&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; &nbsp;0&nbsp;1 |&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; 01&nbsp;2 |&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp;011&nbsp;3 |&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp;011&nbsp;4 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 0100&nbsp;5 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 0101&nbsp;6 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 0110&nbsp;7 |&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; 0111&nbsp;8 |&nbsp; &nbsp; &nbsp; 5&nbsp; &nbsp; &nbsp; &nbsp; | 01000对于更大的数字如果您对更大的数字感兴趣,可以将它们存储在中BigInteger。幸运的是,BigInteger为您提供了一种非常方便的方法:int bitsNeeded(BigInteger number) {&nbsp; &nbsp; &nbsp;return number.bitLength() + 1;}如果您不关心大数字,但想使用bitLength()useint bitsNeeded(long number) {&nbsp; &nbsp; &nbsp;return BigInteger.valueOf(number).bitLength() + 1;}
随时随地看视频慕课网APP

相关分类

Java
我要回答