-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytes.cc
64 lines (54 loc) · 1.07 KB
/
bytes.cc
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
#include "bytes.h"
#include <iostream>
#include <bitset>
Bytes::Bytes(int number)
{
fullNumber = std::bitset<32>(number).to_string();
}
std::string Bytes::to_string()
{
return fullNumber;
}
int Bytes::getNthByte(int nthByte)
{
std::string byteNumber = fullNumber.substr(8 * nthByte, 8);
std::bitset<8> partialNumber(byteNumber);
return (int)partialNumber.to_ulong();
}
int Bytes::findFirstByteIndex()
{
for (int i = 0; i < 4; i++)
if (getNthByte(i) != 0)
return i;
return 3;
}
std::string getVal5Bits(int val)
{
std::bitset<5> bs(val);
return bs.to_string();
}
std::string getVal3Bits(int val)
{
std::bitset<3> bs(val);
return bs.to_string();
}
std::string getVal4Bits(int val)
{
std::bitset<4> bs(val);
return bs.to_string();
}
std::string getVal7Bits(int val)
{
std::bitset<7> bs(val);
return bs.to_string();
}
std::string getVal8Bits(int val)
{
std::bitset<8> bs(val);
return bs.to_string();
}
std::string getVal8BitsSignal(int val)
{
std::bitset<8> bs(val);
return bs.to_string();
}