Convert a decimal to binary in C++
This post will discuss how to convert a decimal to binary in C++.
1. Using std::bitset
function
The standard solution to convert a number to its binary format in C++ is using the std::bitset
class from the <bitset>
header. This class represents a fixed-size sequence of bits, which can be manipulated as a single unit or as individual bits. The bitset class is very useful and convenient, as it provides various methods and features for working with bits and binary numbers.
To use the bitset class to convert a decimal to binary, we need to create a bitset object and initialize it with the decimal value that we want to convert. We can also specify the size of the bitset, which determines how many bits are used to represent the value. We can invoke the to_string
function to get a std::string
representing the bits in the bitset, with leading-zeros. For example, if we want to convert the decimal value 65
to the binary string "01000001"
, we can do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <bitset> int main() { unsigned int decimal = 65; const int n = 8; std::string binary = std::bitset<n>(decimal).to_string(); std::cout << binary << std::endl; // 01000001 return 0; } |
2. Using custom routine
Another way to convert a decimal to binary in C++ is to use the bitwise operators. The bitwise operators are operators that perform operations on the individual bits of a value, such as shifting, masking, or toggling them. To use the bitwise operators to convert a decimal to binary, we need to perform the following steps:
- Create a variable that will store the binary result as a string.
- Create a loop that will iterate over each bit of the decimal value from right to left.
- In each iteration, use the bitwise AND operator
(&)
to extract the rightmost bit of the decimal value. - Use the conditional operator
(?:)
to append either'0'
or'1'
to the binary result string based on the extracted bit. - Use the right shift operator
(>>)
to move the decimal value one bit to the right. - Repeat until the decimal value becomes zero.
For example, if we want to convert the decimal value 65
to the binary value "1000001"
, we can do something like this:
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 |
#include <iostream> #include <string> int main() { // Create a decimal value unsigned int decimal = 65; // Create a binary result string std::string binary; // Loop over each bit of the decimal value from right to left while (decimal > 0) { // Extract the rightmost bit of the decimal value using bitwise AND int bit = decimal & 1; // Append either '0' or '1' to the binary result string based // on the extracted bit binary = (bit == 0 ? '0' : '1') + binary; // Move the decimal value one bit to the right using right shift decimal >>= 1; } // Print the binary result string std::cout << binary << std::endl; // 1000001 return 0; } |
Note that the above solution does not pad the binary string with leading-zeros. We can even write a recursive custom routine that takes an unsigned integer and print its binary digits. The solution can be implemented as follows in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> void toBinary(unsigned int n) { if (n / 2 != 0) { toBinary(n / 2); } std::cout << n % 2; } int main() { unsigned int decimal = 65; toBinary(decimal); // 1000001 return 0; } |
That’s all about converting a decimal to binary in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)