[Java] 이진수의 비트 수(1) 빠르게 세는 방법 알아보기

2022-09-24


geio-tischler-G31Y4f_RIi4-unsplash.jpg
Photo by Geio Tischler on Unsplash

 


1. 방법

 

사실 직접 구현할 필요 없이 아래와 같은 Integer.bitCount(i) 라는 메소드를 사용하면 빠르고 쉽게 구할 수 있다.

 

int num = 12345;
int cntBit = Integer.bitCount(num);
System.out.println(cntBit); //6

 


메인 이미지 출처 : Photo by Geio Tischler on Unsplash