-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
decimal_to_binary.cpp
55 lines (47 loc) · 1.2 KB
/
decimal_to_binary.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
/**
* @file
* @brief Function to convert decimal number to binary representation
*/
#include <iostream>
/**
* This method converts the bit representation and stores it as a decimal
* number.
*/
void method1(int number) {
int remainder, binary = 0, var = 1;
do {
remainder = number % 2;
number = number / 2;
binary = binary + (remainder * var);
var = var * 10;
} while (number > 0);
std::cout << "Method 1 : " << binary << std::endl;
}
/**
* This method stores each bit value from LSB to MSB and then prints them back
* from MSB to LSB
*/
void method2(int number) {
int num_bits = 0;
char bit_string[50];
do {
bool bit = number & 0x01; // get last bit
if (bit)
bit_string[num_bits++] = '1';
else
bit_string[num_bits++] = '0';
number >>= 1; // right shift bit 1 bit
} while (number > 0);
std::cout << "Method 2 : ";
while (num_bits >= 0)
std::cout << bit_string[num_bits--]; // print from MSB to LSB
std::cout << std::endl;
}
int main() {
int number;
std::cout << "Enter a number:";
std::cin >> number;
method1(number);
method2(number);
return 0;
}