-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignExtend.cpp
More file actions
46 lines (40 loc) · 939 Bytes
/
SignExtend.cpp
File metadata and controls
46 lines (40 loc) · 939 Bytes
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
#include <iostream>
#include <bitset>
#include <string>
#include "Components.h"
SignExtend::SignExtend()
{
data_i = bitset<16> (0);
data_o = bitset<32> (0);
}
SignExtend::~SignExtend()
{
}
void SignExtend::input(bitset<16> input){
data_i = input;
data_o = this->extend(data_i);
}
bitset<32> SignExtend::get_output(){
return data_o;
}
bitset<32> SignExtend::extend(bitset<16> input){
/* implement your sign-extend funciton here */
// becareful with the most significant bit.
// if is 0110--> 000...0110
// if is 1100--> 1111..1100
// you have to consider two cases.
string tempstr = input.to_string();
bitset<32> result;
if(input.test(15))
{
string temp1="1111111111111111";
result= bitset<32> ((temp1+tempstr));
return result;
}
else
{
string temp2="0000000000000000";
result= bitset<32> ((temp2+tempstr));
return result;
}
}