-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte.cpp
More file actions
207 lines (185 loc) · 3.09 KB
/
byte.cpp
File metadata and controls
207 lines (185 loc) · 3.09 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "headers.hpp"
using namespace std;
Byte::Byte()
{
this->value = 0;
}
Byte::Byte(string hex_val)
{
unsigned char val = 0;
for(int i = 0; i<2; i++)
{
val = (val<<4);
if(hex_val[i]>='0' && hex_val[i]<='9')
val += hex_val[i]-'0';
else
val += hex_val[i]-'a'+10;
}
this->value = val;
}
Byte::Byte(unsigned char val)
{
this->value = val;
}
unsigned char Byte::getValue() {
return this->value;
}
string Byte::getString()
{
string ans = "";
ans = ans + (char)this->value;
return ans;
}
//+ve = left shift; -ve = right shift
Byte Byte::byteShift(int shift)
{
Byte ans = *this;
if(shift == 0)
return ans;
if(shift<0)
{
shift *= -1;
while(shift--)
ans.value = ans.value/2;
}
else
{
while(shift--)
ans.value = 2*ans.value;
}
return ans;
}
Byte Byte::byteROR(int bits, int shift)
{
Byte ans = *this;
if(bits == 4)
{
while(shift--)
{
bool LSB = ans.getBit(0);
ans = ans.byteShift(-1);
ans = ans & Byte("0f");
string temp = LSB?"08":"00";
ans = ans | Byte(temp);
}
}
else
{
while(shift--)
{
bool LSB = ans.getBit(0);
ans = ans.byteShift(-1);
string temp = LSB?"80":"00";
ans = ans | Byte(temp);
}
}
return ans;
}
Byte Byte::byteROL(int bits, int shift)
{
Byte ans = *this;
if(bits == 4)
{
while(shift--)
{
bool MSB = ans.getBit(3);
ans = ans.byteShift(1);
ans = ans & Byte("0f");
string temp = MSB?"01":"00";
ans = ans | Byte(temp);
}
}
else
{
while(shift--)
{
bool MSB = ans.getBit(7);
ans = ans.byteShift(1);
string temp = MSB?"01":"00";
ans = ans | Byte(temp);
}
}
return ans;
}
Byte Byte::operator|(const Byte &byte)
{
Byte ans = Byte(this->value | byte.value);
return ans;
}
Byte Byte::operator&(const Byte &byte)
{
Byte ans = Byte(this->value & byte.value);
return ans;
}
Byte Byte::operator^(const Byte &byte)
{
Byte ans = Byte(this->value ^ byte.value);
return ans;
}
Byte Byte::operator+(const Byte &byte)
{
int x = this->value, y = byte.value;
int z = (x+y)%(1<<8);
Byte ans = Byte((unsigned char)z);
return ans;
}
Byte Byte::operator*(const Byte &byte)
{
int x = this->value, y = byte.value;
int z = (x*y)%(1<<8);
Byte ans = Byte((unsigned char)z);
return ans;
}
bool Byte::operator>(const Byte &byte)
{
return this->value > byte.value;
}
bool Byte::operator>=(const Byte &byte)
{
return this->value >= byte.value;
}
bool Byte::operator==(const Byte &byte)
{
return this->value == byte.value;
}
Byte& Byte::operator=(const Byte &byte)
{
this->value = byte.value;
return *this;
}
Byte Byte::GFMult(Byte B, unsigned int modulus)
{
unsigned int x = 0;
unsigned int a = this->value, b = B.getValue();
while(a)
{
if(a&1)
x^=b;
b<<=1;
a>>=1;
}
unsigned int y;
modulus <<= 7;
for(int i = 0; i<8; i++)
{
y = x^modulus;
if(y<x)
x = y;
modulus>>=1;
}
return Byte((unsigned char)x);
}
bool Byte::getBit(unsigned int pos)
{
bool bit = (this->value&(1<<pos));
return bit;
}
void Byte::printBits()
{
cout<<bitset<8>(this->value);
}
ostream& operator<<(ostream &out, const Byte &byte)
{
out<<hex<<setfill('0')<<setw(2)<<(unsigned int)byte.value;
return out;
}