-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.h
More file actions
31 lines (26 loc) · 902 Bytes
/
binary.h
File metadata and controls
31 lines (26 loc) · 902 Bytes
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
#include <bitset>
//create a mask st first a digit remain.
//11010101... -> 110000.... for a =2 if we apply the mask
u_int64_t createMask(u_int64_t a){
u_int64_t mask;
//we have to use 1ull because standard shifting in cplusplus works for 32 bit only(?)
mask = ((1ULL << a) - 1)<<(64-a);
//std::bitset<64> b(mask);
//std::cout<<"mask"<<" "<<b<<std::endl;
return mask;
}
u_int64_t getFirstNbits(u_int64_t addr, u_int64_t a){
u_int64_t m = createMask(a);
//11010101... -> 110000.... for a =2 if we apply the mask
u_int64_t value = addr & m;
//std::bitset<64> b(value);
//std::cout<<"value"<<" "<<b<<std::endl;
//shift right to remove the trailing zeros
//110000.... -> 11 for a =2 if we apply the mask
return value>>(64-a);
}
u_int64_t getLastNbits(u_int64_t addr, u_int64_t a){
u_int64_t m;
m = (1ULL << a)-1;
return addr & m;
}