Home > C++ > Printing numbers in binary format in C++

Printing numbers in binary format in C++


The problem

You want to display or output a number in binary format. Iostream only has output manipulators for decimal, hexadecimal and octal.

The bad solution

People usually write a loop to do this that iterates over each bit in the integer:

int v = 0x12345678;
for (int i = 31; i >= 0; i--)
    std::cout << ((v >> i) & 1);

There are many ways to do this; the loop above shifts the bit to print into the least significant bit (bit 0) of a temporary and ANDs it with 1 to remove all the other (higher order) bits and leave a 0 or 1. It starts with the most significant bit (31) and iterates down to the least significant bit (0), ensuring that the bits are printed in the correct order.

The good solution

While a looping solution still has applicability in some other languages, there is a much more elegant way to do it in C++ that is often overlooked:

int v = 0x12345678;
std::cout << std::bitset<32>(v);

The C++ standard library includes a container type bitset whose first non-type template parameter specifies the number of bits to store. It includes an overload for operator<< and a conversion constructor for ints, making printing binary numbers a piece of cake! This is a much preferred solution to the messy for loop construct above.

To use bitset, remember to add:

#include <bitset>

at the top of your code.

Good luck!

Advertisements
Categories: C++ Tags:
  1. Perseus Zepos
    March 7, 2014 at 19:50

    I’m always dumping binary numbers, and bitset is much cleaner than the looping method. Very nice!

  2. Theko
    December 10, 2014 at 06:13

    I have to say that I have been impressed. A very nice tip this page is.

  3. yan
    October 24, 2015 at 17:13

    Lots of luck for you for sharing

  4. sam
    February 16, 2016 at 13:11

    what do I do if I want to write these numbers in a text file??

  5. fdssdf
    March 13, 2016 at 13:37

    bitset obitset.h is not found when I compile

  6. Donald
    May 6, 2016 at 11:33

    Thanks that’s really useful.

  7. Jacek Rużyczka
    November 9, 2016 at 23:13

    Thank you for your hint! I’m currently struggling with the SPI interface of my Raspi and found out that the bit-endianness (MSB first vs. LSB first) was the problem, so I employed this algo to reverse all the data to get it LSB first: http://stackoverflow.com/questions/2602823/in-c-c-whats-the-simplest-way-to-reverse-the-order-of-bits-in-a-byte

    To test this algo, I used your code snippet, and yaaaay, everything works just as it should! *_*

  8. Peter Harding
    December 4, 2016 at 04:50

    Thanks!!

  9. February 14, 2017 at 00:41

    Thank you! It was really helpful! 🙂

  1. August 25, 2014 at 05:33
  2. December 16, 2015 at 22:30

Share your thoughts! Note: to post source code, enclose it in [code lang=...] [/code] tags. Valid values for 'lang' are cpp, csharp, xml, javascript, php etc. To post compiler errors or other text that is best read monospaced, use 'text' as the value for lang.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: