-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintTwosComplement.cpp
55 lines (46 loc) · 1.44 KB
/
printTwosComplement.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
/*
* Program that takes in a decimal integer value and prints the +ve and -ve(2's
* complement) binary form
*
* ./printTwosComplement 3
* Input (decimal): 3
* Positve integer value: 00000000000000000000000000000011
* Negetive integer value: 11111111111111111111111111111101
*
* ./printTwosComplement 1024
* Input (decimal): 1024
* Positve integer value: 00000000000000000000010000000000
* Negetive integer value: 11111111111111111111110000000000
*
*/
#include <iostream>
using namespace std;
void printUsage(char* pgmName)
{
cout << "Program that takes in a decimal integer value and prints the +ve and -ve(2's complement) binary form" << endl;
cout << "Usage: " << pgmName << " IntNum" << endl;
// Note: By default int is a signed int here.
cout << "Input number should be inbetween 0 and 2147483647" << endl;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
printUsage(argv[0]);
return 0;
}
// Note: By default int is a signed int here.
int *posnum = new int(atoi(argv[1]));
// Any number greater than 2147483647 will be a -ve number here
if (*posnum < 0 )
{
printUsage(argv[0]);
return 0;
}
signed int *negnum = new signed int(-*posnum);
cout << "Input (decimal): " << *posnum << endl;
cout << "Positve integer value: " << std::bitset<32>(*posnum) << endl;
cout << "Negetive integer value: " << std::bitset<32>(*negnum) << endl;
delete posnum;
delete negnum;
}