반응형

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.1 | |
/* | |
* 두 개의 32비트 수 N과 M이 주어지고, 비트 위치 i와 j가 주어졌을 때, M을 N에 삽입하는 메서드를 구현하라. | |
* M은 N의 j번째 비트에서 시작하여 i번째 비트에서 끝나야 한다. j번째 비트에서 시작하여 i번째 비트에서 끝나야 한다. | |
* j번째 비트에서 i번째 비트까지에는 M을 담기 충분한 공간이 있다고 가정한다. | |
*/ | |
public static int Q1_InsertBits(int n, int m, int i, int j) | |
{ | |
var left = ~0 << (j + 1); | |
var right = ((1 << i) - 1); | |
var mask = left | right; | |
var nCleared = n & mask; | |
var mShifted = m << i; | |
return nCleared | mShifted; | |
} | |
#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_1() | |
{ | |
Assert.AreEqual(0x0000044C, BitManipulation.Q1_InsertBits(0x00000400, 0x00000013, 2, 6)); | |
Assert.AreEqual(0x00000413, BitManipulation.Q1_InsertBits(0x00000400, 0x00000013, 0, 4)); | |
Assert.AreEqual(0x000BFFFF, BitManipulation.Q1_InsertBits(0x000B0000, 0x0000FFFF, 0, 15)); | |
} |
반응형
'Develop > 코딩인터뷰' 카테고리의 다른 글
[코딩인터뷰] 문제 5.3 (0) | 2020.04.02 |
---|---|
[코딩인터뷰] 문제 5.2 (0) | 2020.04.02 |
[코딩인터뷰] 문제 4.9 (0) | 2018.02.10 |
[코딩인터뷰] 문제 4.8 (0) | 2018.02.10 |
[코딩인터뷰] 문제 4.7 (0) | 2018.02.10 |
꾸준히 노력하는 개발자 "김예건" 입니다.