[Java] bitCount() 주어진 정수에서 true bit 개수를 찾는법

WooHyunLEE·2023년 8월 25일
0

bitCount()

Integer.bitCount

정수를 매개변수로 넣었을때 그 정수의 1의 개수를 반환한다.

프로그래머스 LV.2 다음 큰 숫자

https://school.programmers.co.kr/learn/courses/30/lessons/12911

class Solution {
    public int solution(int n) {
        int answer = n+1;

        int nBit = Integer.bitCount(n);


        while (true) {
            int ansBit = Integer.bitCount(answer);

            if (ansBit == nBit) {
                return answer;
            } else {
                answer++;
            }
        }

    }
}

참고

https://blog.yevgnenll.me/posts/java-integer-bit-count-function

profile
이우현의 개발 블로그입니다.

0개의 댓글