decimal_to_binary.cpp File Reference

Function to convert decimal number to binary representation. More...

`#include <iostream>`
Include dependency graph for decimal_to_binary.cpp:

## Functions

void | method1 (int number)
void | method2 (int number)
int | main ()

## Detailed Description

Function to convert decimal number to binary representation.

## Function Documentation

## method1()

void method1 | ( | int | number | ) |

This method converts the bit representation and stores it as a decimal 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;
}
std::cout
std::endl
T endl(T... args)
std::remainder
T remainder(T... args)
Here is the call graph for this function:

## method2()

void method2 | ( | int | number | ) |

This method stores each bit value from LSB to MSB and then prints them back from MSB to LSB

{
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;
}
Here is the call graph for this function: