-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_1.cpp
55 lines (49 loc) · 917 Bytes
/
5_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// 5_1.cpp
//
//
// Created by Pengyan Qin on 7/3/15.
//
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void print(int n){
vector<int> v;
int len = 8*sizeof(n);
int mask = 1;
while(len--){
if(mask&n)
v.push_back(1);
else
v.push_back(0);
n = n >> 1;
}
while(!v.empty()){
cout << v.back();
v.pop_back();
}
cout << endl;
}
int insert_num(int N, int M, int i, int j){ // insert M into N from i to j
int mask1 = (-1 << (j+1)) | ((1 << i) - 1); // 1111100001111
print(mask1);
N &= mask1;
print(N);
M <<= i;
print(M);
N = M | N;
return N;
}
int main(){
int N = 129274611;
print(N);
int M = 35;
print(M);
int i = 4;
int j = 9;
int res = insert_num(N, M, i, j);
print(res);
}
// print(~0) and print(-1) is all 1