본문 바로가기

Develop/코딩인터뷰

[코딩인터뷰] 문제 5.5

반응형

코딩 인터뷰
문제 5.5

#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
view raw Code_Q5_5.cs hosted with ❤ by GitHub
[TestMethod]
public void Q5_5()
{
Assert.AreEqual(0, BitManipulation.Q5_BitSwapRequired(0x000000E2, 0x000000E2));
Assert.AreEqual(4, BitManipulation.Q5_BitSwapRequired(0x00000860, 0x0000F860));
}
view raw Test_Q5_5.cs hosted with ❤ by GitHub
반응형

'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