-
Notifications
You must be signed in to change notification settings - Fork 275
/
1's&2'sCompliment.cpp
55 lines (53 loc) · 1.09 KB
/
1's&2'sCompliment.cpp
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
#include <iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
int len=8;
char bin[len],one[len],two[len];
cout<<"Enter the Binary Number:";
gets(bin);
int flag =0;
for(int i=0;i<len;i++)//one's compliment
{
if(bin[i]=='1')
{
one[i]='0';
}
else if(bin[i]=='0')
{
one[i]='1';
}
else
{
cout<<"Invalid Binary Digits";
flag=1;
return 0;
}
}
int carry=1;
for(int i=len-1;i>=0;i--)//two's compliment
{
if(one[i]=='1' && carry==1)
{
two[i]='0';
}
else if(one[i]=='0' && carry==1)
{
two[i]='1';
carry=0;
}
else
{
two[i]=one[i];
}
}
if(flag==0)
{
cout<<"Binary number is:"<<bin<<endl;
cout<<"1's Compliment of the number is:"<<one<<endl;
cout<<"2's Compliment of the number is:"<<two<<endl;
}
return 0;
}