How to Convert Decimal to Binary in C++

Jinku Hu Feb 02, 2024
  1. Use Custom Defined Function to Convert Decimal Number to Binary in C++
  2. Use std::bitset Class to Convert Decimal Number to Binary in C++
How to Convert Decimal to Binary in C++

This article will introduce several methods of how to convert decimal numbers to binary representation in C++.

Use Custom Defined Function to Convert Decimal Number to Binary in C++

The flexible method defines a function that takes the int value, converts it to corresponding binary representation, and returns it as a string value. In this case, we implemented the algorithm using modulo % operator and while loop, where we decrease the value of integer by half each iteration.

#include <iostream>

using std::cout;
using std::endl;
using std::string;

string toBinary(int n) {
  string r;
  while (n != 0) {
    r += (n % 2 == 0 ? "0" : "1");
    n /= 2;
  }
  return r;
}

int main() {
  int number = 15;

  cout << "decimal: " << number << endl;
  cout << "binary : " << toBinary(number) << endl;

  return EXIT_SUCCESS;
}

Output:

decimal: 15
binary : 1111

Use std::bitset Class to Convert Decimal Number to Binary in C++

Alternatively, we can directly use the bitset class from the STL library. bitset represents a fixed-size sequence of N bits, and it offers multiple built-in methods to manipulate the binary data efficiently. The following example shows the bitset object construction by passing the string value and an int value.

#include <bitset>
#include <iostream>

using std::bitset;
using std::cout;
using std::endl;
using std::string;

string toBinary(int n) {
  string r;
  while (n != 0) {
    r += (n % 2 == 0 ? "0" : "1");
    n /= 2;
  }
  return r;
}

int main() {
  int number = 15;

  bitset<32> bs1(toBinary(number));
  cout << "binary:  " << bs1 << endl;
  bitset<32> bs2(number);
  cout << "binary:  " << bs2 << endl;

  return EXIT_SUCCESS;
}

Output:

binary:  00000000000000000000000000001111
binary:  00000000000000000000000000001111

Notice that we specified 32 bits to be allocated during the bitset declaration in the previous code. We can specify a different number of bits to suit their needs better. Multiple scenarios are demonstrated in the following code segment:

#include <bitset>
#include <iostream>

using std::bitset;
using std::cout;
using std::endl;
using std::string;

int main() {
  int number = 15;

  bitset<32> bs1(number);
  cout << "binary:  " << bs1 << endl;
  bitset<16> bs2(number);
  cout << "binary:  " << bs2 << endl;
  bitset<8> bs3(number);
  cout << "binary:  " << bs3 << endl;
  bitset<5> bs4(number);
  cout << "binary:  " << bs4 << endl;
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

binary:  00000000000000000000000000001111
binary:  0000000000001111
binary:  00001111
binary:  01111

bitset class has several useful methods to operate on its content. These methods can be employed to invert all bits of the set (flip function) or to reset/set the specified bits in the sequence. The core binary operations like AND, OR, XOR, NOT and SHIFT are also supported. We show a couple of them in the example below, but you can see the full manual of the bitset class here.

#include <bitset>
#include <iostream>

using std::bitset;
using std::cout;
using std::endl;
using std::string;

int main() {
  int number = 15;

  bitset<32> bs(number);
  cout << "binary : " << bs << endl;
  cout << "flipped: " << bs.flip() << endl;
  cout << "shift>4: " << (bs >>= 6) << endl;
  cout << "shift<5: " << (bs << 2) << endl;
  cout << "reset  : " << bs.reset() << endl;
  cout << "set    : " << bs.set(16) << endl;

  return EXIT_SUCCESS;
}

Output:

binary : 00000000000000000000000000001111
flipped: 11111111111111111111111111110000
shift>4: 00000011111111111111111111111111
shift<5: 00001111111111111111111111111100
reset  : 00000000000000000000000000000000
set    : 00000000000000010000000000000000
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Binary