Here you can find the source of bitSizeForSignedValue(final int value)
public static int bitSizeForSignedValue(final int value)
//package com.java2s; //License from project: Open Source License public class Main { /**// w w w .ja va 2 s . c o m * @return The number of bits required to store the specified value as a signed integer, i.e. a result in [1,32]. */ public static int bitSizeForSignedValue(final int value) { if (value > 0) { return 33 - Integer.numberOfLeadingZeros(value); } else if (value == 0) { return 1; } else { // Works for Integer.MIN_VALUE as well. return 33 - Integer.numberOfLeadingZeros(-value - 1); } } /** * @return The number of bits required to store the specified value as a signed integer, i.e. a result in [1,64]. */ public static int bitSizeForSignedValue(final long value) { if (value > 0) { return 65 - Long.numberOfLeadingZeros(value); } else if (value == 0) { return 1; } else { // Works for Long.MIN_VALUE as well. return 65 - Long.numberOfLeadingZeros(-value - 1); } } }