Decimal to Binary in C++
What is the meaning of Decimal Numbers?
Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two digits is a decimal number, such as 121, 654, 111, 0, 3, and so on.
What is the meaning of Binary Numbers?
Because it is either 0 or 1, a binary number is a base 2 number. Binary numbers are made up of 0 and 1 digit, such as 10001, 1001, 1111, 101110, and so on.
Example:
// C++ program to convert a decimal
// number to binary number
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
// function to convert decimal to binary
void decimalToBinary ( int n )
{
// array to store binary number
int binaryNumber [ 32 ] ;
// counter for binary array
int i = 0 ;
while ( n > 0 ) {
// storing remainder in binary array
binaryNumber [ i ] = n % 2 ;
n = n / 2 ;
i++ ;
}
// printing binary array in reverse order
for ( int j = i - 1 ; j >= 0 ; j-- )
cout << binaryNumber [ j ] ;
}
int main ( )
{
int n = 10 ;
decimalToBinary ( n ) ;
return 0 ;
}
OUTPUT:
1010
Explanation:
- When 10 is divided by 2, the remainder is zero. As a result, arr [ 0 ] = 0.
- Subtract 2 from 10. 10 divided by 2 is equal to 5 is the new number.
- When you divide five by two, the remainder is 1. As a result, arr [ 1 ] = 1.
- Subtract 2 from 5. 5 / 2 = 2 is the new number.
- When 2 is divided by 2, the remainder is zero. As a result, arr [ 2 ] = 0.
- Subtract 2 from 2. 2 / 2 = 1 is the new number.
- When you divide 1 by 2, the remainder is 1. As a result, arr [ 3 ] = 1.
- Subtract 1 from 2. 1 / 2 = 0 is the new number.
- Since the number equals 0 reverse the order of the array. As a result, the binary equivalent is 1010.
NOTE: To do the following, we can utilize bitwise operators. Bitwise operators are quicker than the arithmetic operators used previously.
Another example but now using bitwise operator:
// C++program to Decimal to binary conversion
// using bitwise operator
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
// Function that convert Decimal to binary
int decimalToBinary ( int n )
{
// Size of an integer is assumed to be 32 bits
for ( int i = 31 ; i > = 0 ; i-- ) {
int k = n >> i ;
if ( k & 1 )
cout << " 1 " ;
else
cout << " 0 " ;
}
}
// driver code
int main ( )
{
int n = 64 ;
decimalToBinary ( n ) ;
}
OUTPUT:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
Explanation:
Even for tiny numbers, the loop iterates a constant (64) amount of times.
Better Approach for Decimal to Binary in C++
Using the right shift (> >) and And ( & ) operators, it's another quick way to convert Decimal to binary. We'll solely utilize Binary Operators here, which are often quite quick in calculation.
#include < bits/stdc++.h >
#include < stdlib >
#include < iostream >
using namespace std ;
string DecimalToBinary ( int num )
{
string str ;
while ( num ) {
if ( num & 1 ) // 1
str += '1' ;
else // 0
str += '0' ;
num >>= 1 ; // Right Shift by 1
}
return str ;
}
void rev ( string str )
{
for ( int i = str.size ( ) - 1 ; i >= 0 ; i-- )
cout << str [ i ] ;
}
int main ( ) {
int num = 49 ;
cout << "Binary of number” << num << ” is: " ;
rev ( DecimalToBinary ( num ) ) ;
return 0 ;
}
OUTPUT:
Binary of Number 49 is 110001
………………………………………………
Process executed in 0.321 seconds
Press any key to continue.
Explanation:
Above code is found to be a better approach of converting decimal to binary in C++. Using a string function DecimalTobinary and passing the number we want to convert and also using a function called rev to reverse.