-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMT2019030_Bitset.cpp
75 lines (69 loc) · 2.13 KB
/
IMT2019030_Bitset.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "IMT2019030_Bitset.h"
//Constructors and Destructors
Bitset::Bitset(){}
Bitset::Bitset(string bits){
this->bitset=bits;
}
Bitset::Bitset(int size){
string s(size,'0');
this->bitset=s;
}
Bitset::Bitset(const Bitset&oldObj){
this->bitset.clear();
this->bitset=oldObj.bitset;
}
Bitset::~Bitset(){
this->bitset.clear();
}
//Getters and Setters
int Bitset::size(){
return this->bitset.size();
}
void Bitset::setBitset(string newBitset){
this->bitset=newBitset;
}
string Bitset::stringform(){
return this->bitset;
}
//Member functions
Bitset Bitset::extract(int leastSign,int mostSign){
Bitset b(this->bitset.substr(this->bitset.size()-1-mostSign,mostSign-leastSign+1));
return b;
}
string Bitset::extractString(int leastSign,int mostSign){
return this->bitset.substr(this->bitset.size()-1-mostSign,mostSign-leastSign+1);
}
Bitset Bitset::HexToBin(string Hex){
map<char,string>HexToBinHelper;
HexToBinHelper.insert(pair<char,string>('0',"0000"));
HexToBinHelper.insert(pair<char,string>('1',"0001"));
HexToBinHelper.insert(pair<char,string>('2',"0010"));
HexToBinHelper.insert(pair<char,string>('3',"0011"));
HexToBinHelper.insert(pair<char,string>('4',"0100"));
HexToBinHelper.insert(pair<char,string>('5',"0101"));
HexToBinHelper.insert(pair<char,string>('6',"0110"));
HexToBinHelper.insert(pair<char,string>('7',"0111"));
HexToBinHelper.insert(pair<char,string>('8',"1000"));
HexToBinHelper.insert(pair<char,string>('9',"1001"));
HexToBinHelper.insert(pair<char,string>('a',"1010"));
HexToBinHelper.insert(pair<char,string>('b',"1011"));
HexToBinHelper.insert(pair<char,string>('c',"1100"));
HexToBinHelper.insert(pair<char,string>('d',"1101"));
HexToBinHelper.insert(pair<char,string>('e',"1110"));
HexToBinHelper.insert(pair<char,string>('f',"1111"));
string s="";
for(auto h:Hex){
s+=HexToBinHelper[h];
}
Bitset b(s);
return b;
}
int Bitset::BinToDec(Bitset bin){
int dec=0;
int twoPow=1;
for(int i=bin.size()-1;i>=0;i--){
if(bin.stringform()[i]=='1')dec+=twoPow;
twoPow*=2;
}
return dec;
}