반응형

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Question 5.5 | |
/* | |
* 정수 A를 B로 변환하기 위해 바꿔야 하는 비트 개수를 계산하는 함수를 작성하라. | |
*/ | |
public static int Q5_BitSwapRequired(int a, int b) | |
{ | |
int c = a ^ b; | |
int count = 0; | |
while(c != 0) | |
{ | |
c &= c - 1; | |
count++; | |
} | |
return count; | |
} | |
#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void Q5_5() | |
{ | |
Assert.AreEqual(0, BitManipulation.Q5_BitSwapRequired(0x000000E2, 0x000000E2)); | |
Assert.AreEqual(4, BitManipulation.Q5_BitSwapRequired(0x00000860, 0x0000F860)); | |
} |
반응형
'Develop > 코딩인터뷰' 카테고리의 다른 글
[코딩인터뷰] 문제 5.7 (0) | 2020.04.02 |
---|---|
[코딩인터뷰] 문제 5.6 (0) | 2020.04.02 |
[코딩인터뷰] 문제 5.4 (0) | 2020.04.02 |
[코딩인터뷰] 문제 5.3 (0) | 2020.04.02 |
[코딩인터뷰] 문제 5.2 (0) | 2020.04.02 |
꾸준히 노력하는 개발자 "김예건" 입니다.