forked from nykez/memory-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitUtilities.cpp
34 lines (32 loc) · 1.25 KB
/
BitUtilities.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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Project: GroupProject
// File Name: PageTable.cpp
// Description: Implementation of BitUtilities.h
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "BitUtilities.h"
/// <summary>
/// FROM: From https://www.geeksforgeeks.org/extract-k-bits-given-position-number/
/// Function to extract k bits from p position
/// and returns the extracted value as integer
/// So, for p = 0 and k = 3, starting from bit 0, we take the next 3 bits on the left
/// including bit 0. So, for 1010 0010, we return 1010 0[010] : the part in brackets
/// For p = 1 and k = 3, we get 1010 [001]0.
/// For p = 3 and k = 3, we get 10[10 0]010.
/// For p = 1 and k = 2, we get 10100 [01]0.
/// </summary>
int BitUtilities::ExtractBits(int number, int k, int p) {
return (((1 << k) - 1) & (number >> (p)));
}
/// <summary>
///
/// </summary>
int BitUtilities::CreateBitMasking(int startBit, int endBit)
{
int mask = 0;
for (int i = startBit; i <= endBit; ++i) {
mask |= 1 << i;
}
return mask;
}