본문 바로가기
코틀린/[Mapping] 매핑 작업

[Kotlin][Integer] toBinaryString

by jinwo_o 2024. 9. 7.

정수 인수의 문자열 표현을 기수 2의 부호 없는 정수로 반환합니다.

public static String toBinaryString(int i) {
    return toUnsignedString0(i, 1);
}

private static String toUnsignedString0(int val, int shift) {
    // assert shift > 0 && shift <=5 : "Illegal shift value";
    int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
    int chars = Math.max(((mag + (shift - 1)) / shift), 1);
    if (COMPACT_STRINGS) {
        byte[] buf = new byte[chars];
        formatUnsignedInt(val, shift, buf, chars);
        return new String(buf, LATIN1);
    } else {
        byte[] buf = new byte[chars * 2];
        formatUnsignedIntUTF16(val, shift, buf, chars);
        return new String(buf, UTF16);
    }
}


print(Integer.toBinaryString(5)) // 101

 

Integer (Java Platform SE 8 )

Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits. (Bits shifted out of the left hand, or high-order, side reenter on the right, or low-order.) Note that left r

docs.oracle.com

'코틀린 > [Mapping] 매핑 작업' 카테고리의 다른 글

[Kotlin][Collection] getOrPut  (0) 2024.09.07
[Kotlin][Map] getOrDefault  (0) 2024.09.07
[Kotlin][String] toBigDecimal  (0) 2024.09.04