. Advertisement .

..3..

. Advertisement .

..4..

Many still struggle while converting **decimal to binary in C++**. Is there a proper yet simple method to complete this work with little effort? Let’s find out with this post!

**What Is Decimal And Binary Number?**

The decimal number is the base-10 number system, which has a total of 10 digits ranging from 0 to 9. Any digits combination is a decimal, for example, 565, 23, 889, 334, etc.

The binary number is the base-2 number system, containing two digits, 0 and 1. Any of these two number combinations is a binary number, for instance, 110, 10101, 11, 10001, etc.

**Method 1: Convert Decimal To Binary In C++ Utilizing Custom-Defined Function**

The versatile method specifies the function that accepts a value int, converts this value to its appropriate binary representation, then gives back the result as the string value.

In this instance, we constructed the technique using the modulo operator % with loop while, reducing the integer’s value by 50% after each repetition.

```
#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
```

**Method 2: Convert Decimal To Binary In C++ Utilizing std::bitset**

As an alternative, you can utilize the STL library’s class bitset directly. In order to efficiently manipulate the binary data, this class represents a fixed-size N bits sequence and includes a number of built-in functions.

The below sample will demonstrate the construction of the object bitset by passing a value int and the value string.

```
#include <iostream>
#include <bitset>
using std::cout; using std::endl;
using std::string; using std::bitset;
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
```

As you can see, in the previous code’s bitset declaration, we stated that 32 bits would be allocated. To better meet their demands, we can provide a different bit count.

The code segment below demonstrates several scenarios.

```
#include <iostream>
#include <bitset>
using std::cout; using std::endl;
using std::string; using std::bitset;
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
```

The class bitset contains a number of practical methods for manipulating its data. These techniques can be used to invert every bit in the set (function flip) or to set/reset the sequence’s designated bits. The basic binary operations such as SHIFT, NOT, XOR, AND, and OR are also available.

You can use the following sample as a reference.

```
#include <iostream>
#include <bitset>
using std::cout; using std::endl;
using std::string; using std::bitset;
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
```

**The Bottom Line**

The information above should be sufficient to let you convert **decimal to binary in C++** programming language. Put these approaches into practice and see which one suits best for your demands.As a C++ programmer, you will need to gain more skills continuously. Check out our site, where you find numerous useful tutorials such as parsing a string with a delimiter, converting a char to a string, etc.

## Leave a comment