-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex26.c++
52 lines (46 loc) · 1.13 KB
/
ex26.c++
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
/**
26.
Write a function to read in an integer and print the representation of the number
using the sign and magnitude representation scheme using 8 bits. The program should
check for overflow/under flow conditions. The left most bit is to be used as the sign
bit.
*/
#include<iostream>
#include<cmath>
using namespace std;
void signed_bit_represenation(int dec_num)
{
int bin[8] = {0};
bool flag_overflow = false;
//sign bit set
int sign_bit = bin[0] = (dec_num < 0)? 1:0; //as lsb is the sign bit
//only magnitude required
int n = fabs(dec_num);
int i = 8;
while (n > 0) {
// storing remainder in binary array
bin[i] = n % 2;
n = n / 2;
i--;
}
//checking for overflow condition
if(sign_bit == bin[0])
{
//display the binary number
for(int j=0; j<8; j++)
{
cout<<bin[j];
}
cout<<"\n";
}
else cout<<"overflow occoured!!!";
}
int main(int argc, char const *argv[])
{
int n;
cout<<"Input a number :";
cin>>n;
cout<<"The signed-magnitude representation of `"<<n<<"` is :";
signed_bit_represenation(n);
return 0;
}