-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedLibrary.hpp
83 lines (74 loc) · 1.92 KB
/
sharedLibrary.hpp
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
76
77
78
79
80
81
82
83
//
// This is shared code between the Assembler
// and the Manchester Baby computer
// Pui-Hin Vincent Lee: 180006178
// Alina Littek: 180016182
// Bjarne Kopplin: 180016866
// Jacque Nalwanga: 180004882
// Andrew Spence: 170018476
//
#ifndef MBASSEMBLER_SHAREDLIBRARY_HPP
#define MBASSEMBLER_SHAREDLIBRARY_HPP
#include <bitset>
#include <algorithm>
/**
* Truns a long into a binary output
* @param longInput the long you want a boolean version of
* @return the string containing 1/0
*/
string toBinary(long longInput){
return bitset<32>(longInput).to_string();
}
/**
* Turns number into 3 bits
* @param inputInt input number
* @return string binary
*/
string instructionToBinary(int inputInt){
if(inputInt > 7){
throw INPUT_TOO_BIG;
}
return bitset<3>(inputInt).to_string();
}
/**
* Turns number into 5 bits
* @param inputInt input number
* @return string binary
*/
string memoryLocationToBinary(int inputInt){
if(inputInt > 31){
throw INPUT_TOO_BIG;
}
return bitset<5>(inputInt).to_string();
}
/**
* turns an int into binary with no trailing 0s
* @param inputInt input number
* @return string binary
*/
string intToBinary(int inputInt){
string binary = bitset<32>(inputInt).to_string();
bool hitAOne = false;
string finaloutput = "";
//this for loop removes any trailing 0s
for(auto &l : binary){
if(l != '0'){ //we wait till we hey a char that's a 1 before doing anything
finaloutput += l;
hitAOne = true;
}else if(hitAOne){ // we accept all 1/0 after we hit the first 1
finaloutput += l;
}
}
return finaloutput;
}
/**
* Reverses a string
* @param stringIn string to be reversed
* @return the reversed string
*/
string reverseString(string stringIn){
string temp = stringIn;
reverse(temp.begin(), temp.end()); //reverses string
return temp;
}
#endif //MBASSEMBLER_SHAREDLIBRARY_HPP