Notice
Recent Posts
Recent Comments
Link
목록2023/09/01 (2)
넘치게 채우기
비트 연산 : 십진수의 이진수 표기에서의 1의 개수 구하기
십진수를 이진수로 표기하고, 이진수 표기에서 1을 구하고 싶다면, 십진수 i를 2로 나눈 값 i/2의 1의 개수에, i가 홀수면 1을 더하고, 짝수인 경우 더하지 않으면 된다. #include // 십진수를 이진수로 변환하고 1의 개수를 구하는 함수 int countOnesInBinary(int n) { int count = 0; while (n > 0) { count += n % 2; // 현재 비트가 1이면 count에 1을 더함 n /= 2; // 십진수를 2로 나눔 } return count; } int main() { int i = 25; // 예시로 25를 사용 int binaryCount = countOnesInBinary(i); std::cout
컴퓨터과학/비트마스킹
2023. 9. 1. 23:08
[LeetCode] 338. Counting Bits
https://leetcode.com/problems/counting-bits/description/ Counting Bits - LeetCode Can you solve this real interview question? Counting Bits - Given an integer n, return an array ans of length n + 1 such that for each i (0 1] + (i & 1); } return table; } };
PS/LeetCode
2023. 9. 1. 16:55