Convert a number to a binary string (and back) in C++

Sometimes it’s useful to output the binary representation of a number in text, i.e. as an ASCII string of 0’s and 1’s. There are also situations where you might want convert back the other way, e.g. if you want to let a user enter a binary string manually. The bitset class in C++ makes this surprisingly quick and easy.

Number to binary string

The first thing to do is include the <bitset> header in your code. In the examples below, we’re outputting/storing the number 123 as an 8-bit value.

Output directly to the console (or other output stream):

std::cout << std::bitset<8>(123);

Store in a string object:

std::string str = std::bitset<8>(123).to_string();

Note that the template parameter (the number between <angle brackets>) specifies the number of bits you want to use. You have to make sure it’s big enough to hold the number you pass into the constructor.

Don’t just use a huge number of bits though, otherwise you could end up with a lot of leading 0’s in your output. It’s also important to note that the number you pass into the constructor will have a finite limit. It should be able to handle at least a 32-bit unsigned integer according to the standard though (a little over 4 billion).

Binary string to number

Going back the other way (from a binary string to a number) is just as easy:

std::cout << std::bitset<8>("11011011").to_ulong();

That will output the number 219 directly to the console. Alternatively, you can also store the result in a variable:

unsigned long n = std::bitset<8>("11011011").to_ulong();

In both cases, you could also pass in a string object rather than a string lteral.

Once again, these examples are using an 8-bit number. A notable limitation here is that the number of bits is a compile-time value. That means you can’t determine the length of the string at run-time and use that number of bits. You have to determine in advance how many you’re likely to need.

Explanation

The bitset class is designed to represent a binary field where specific bits may be turned on or off like flags. This can be useful for various things such as storing configuration information.

It’s quite common to store this information as plain old integer values, using a combination of bitwise operators to adjust the individual bits. This approach can be seen in things like the Win32 API, and is still very commonly used in C programming. It’s not always intuitive though, and can be tricky to follow if you’re not used to it. The bitset class makes it much easier and hopefully less prone to mistakes.

In the examples above, we’re simply using some helpful string-related functionality from the bitset class. I wouldn’t recommend using these functions for most bit-manipulation. They’re simply helpful time-savers for certain situations. If you need to use bitsets more extensively, I recommend reading formal documentation for details.

Leave a Reply

Your email address will not be published. Required fields are marked *