정리해뒀던 글에 덧붙여서..
비트 연산과 비트 마스크 (24.09.23)
오늘 풀었던 문제가
비트 연산을 활용해 풀 수 있다는 것을 공부했고 여기에 정리해둔다.
#include <string>
using namespace std;
int countOnes(int m){
int cnt = 0; // 1의 갯수
while(m>0)
{
if(m&1) cnt++; // n의 비트 끝자리가 1인 경우
m = m >> 1; // n의 비트를 오른쪽으로 한 칸 이동
}
return cnt;
}
int solution(int n) {
int i = 1;
while(1)
{
if(countOnes(n) == countOnes(n+i)) return n+i;
i++;
}
}
