Binary Representation of Numbers using C++
Bitset class and Bitwise Operator in C++ can convert a given number from decimal to binary. Let’s understand the Binary Representation of Numbers with relevant examples.
In C++, the binary representation of numbers can be expressed using the binary literal prefix “0b”. The binary literal prefix is used to specify a value in binary format.
This tutorial will focus on various methods to convert decimal numbers to binary format, specifically using iterative and recursive methods in C++. Additionally, the article covers how the Bitset class and Bitwise Operator in C++ can convert a given number from decimal to binary format. Let’s begin with our topic Binary representation of numbers in C++.
Explore: C++ Courses
We will be covering the following sections:
- What are Binary and Decimal Numbers?
- C++ program to convert Decimal Number to Binary Number
- C++ program to convert Decimal Number to Binary Number Using Bitwise Operator
- C++ program to convert Decimal Number to Binary Number Using Bitset Class
Also read: New and delete operators in C++
What are Binary and Decimal Numbers?
Binary and decimal are two different systems for representing numbers.
Decimal is a base-10 numbering system that uses 10 digits, from 0 to 9, to represent all possible values. In the decimal system, each digit represents a power of 10, with the rightmost digit representing 10^0 (or 1), the next representing 10^1 (or 10), the next representing 10^2 (or 100), and so on. For example, the number 1234 in decimal notation represents 1 x 10^3 + 2 x 10^2 + 3 x 10^1 + 4 x 10^0.
Binary, on the other hand, is a base-2 numbering system that uses only two digits, 0 and 1, to represent all possible values. In the binary system, each digit shows a power of 2, with the rightmost digit representing 2^0 (or 1), the next representing 2^1 (or 2), the next representing 2^2 (or 4), and so on. For example, the binary number 1010 represents 1 x 2^3 + 0 x 2^2 + 1 x 2^1 + 0 x 2^0, which simplifies to 8 + 0 + 2 + 0, or 10 in decimal notation.
The binary is commonly used in digital electronics and computing, providing a simple and efficient way to represent and manipulate data in a computer’s memory and circuits. Decimal, on the other hand, is the most commonly used numbering system for everyday arithmetic and calculations.
You can also explore: Convert char to int in C++ Using Different Methods
Let’s learn the binary representation of numbers using a C++ program and convert Decimal Numbers to Binary Numbers.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
C++ program to convert Decimal Number to Binary Number
Here are two methods for converting a decimal number to a binary number:
- Iterative Method
- Recursive Method
You can also explore: Ceil function in C++
Iterative Method
#include <iostream>
using namespace std;
int main() {
int decimal, quotient, remainder, binary = 0, place = 1;
cout << "Enter a decimal number: ";
cin >> decimal;
quotient = decimal;
while (quotient != 0) {
remainder = quotient % 2;
binary += remainder * place;
place *= 10;
quotient /= 2;
}
cout << "The binary equivalent of " << decimal << " is " << binary << endl;
return 0;
}
Output:
In this program, we first take input from the user for the decimal number to be converted. We then use a while loop to repeatedly divide the decimal number by 2 and calculate its remainder to obtain the binary digits in reverse order. We use the variable “place” to keep track of the current place value of the binary digit being calculated, and multiply the remainder by this place value to add it to the binary number. Finally, we print the binary equivalent of the decimal number.
You can also explore: Multilevel inheritance in C++ with real life analogy
Recursive Method
#include <iostream>
using namespace std;
void decimalToBinary(int decimal) {
if (decimal == 0) {
return;
} else {
decimalToBinary(decimal / 2);
cout << decimal % 2;
}
}
int main() {
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
cout << "The binary equivalent of " << decimal << " is ";
decimalToBinary(decimal);
cout << endl;
return 0;
}
Output:
In this program, we define a recursive function decimalToBinary that takes a decimal number as input and recursively calls itself with the decimal number divided by 2 until the decimal number becomes 0. At each recursive call, we print the remainder obtained by dividing the decimal number by 2, representing the binary digit in reverse order. Finally, we print the binary equivalent of the decimal number in the main function by calling the decimalToBinary function.
Note: The recursive approach, the binary digits are printed in reverse order, so we need to reverse the order to get the correct binary representation of the decimal number.
C++ program to Convert Decimal Number to Binary Number Using Bitwise Operator
Binary operators can also be used to convert decimal numbers into binary numbers.
The Binary Shift Right Operator (>>) is a binary operator requiring two operands: x and y. The value of x is the integer we want to shift the bits of, while y is the number of positions we want to shift the bits by.
Shifting the bits of x by y positions towards the right is the same as dividing x by 2 to the power of y.
Here is an example of using the bitwise operator to convert decimal to binary through recursive method:
Output:
In this program, we define a recursive function decimalToBinary that takes a decimal number as input and recursively calls itself with the decimal number right-shifted by 1 (i.e. divided by 2) until the decimal number becomes 1 or 0. At each recursive call, we print the least significant bit of the decimal number obtained by applying the bitwise AND operator with 1, which represents the binary digit in reverse order. Finally, we print the binary equivalent of the decimal number in the main function by calling the decimalToBinary function.
Note: The bitwise operator approach uses less memory and is more efficient than the iterative approach, but it may be less intuitive to understand for beginners.
C++ program to convert Decimal Number to Binary Number Using Bitset Class
The Bitset class in C++ is designed to store boolean values of either 0 or 1, which represent true or false. This class can hold both positive and negative integers. One of the main benefits of using the Bitset class is that it allows us to save the binary representation of a given decimal number in either 8-bit, 32-bit, or any other user-defined bit representation. When using the Bitset class, we can access each individual bit by using the array indexing operator. The indexing for this class is performed from right to left, with the rightmost digit having an index of 0. The size of the bitset is predetermined during compile time and cannot be modified during runtime.
Here is an example of using the C++ bitset class to convert decimal to binary:
#include <iostream>
#include <bitset>
using namespace std;
int main() {
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
bitset<8> binary(decimal);
cout << "The binary equivalent of " << decimal << " is " << binary << endl;
return 0;
}
Output:
This program first prompts the user to enter a decimal number. Then, we create a bitset object named binary with a size of 8 bits, which is used to store the binary representation of the decimal number. We pass the decimal number as an argument to the constructor of the bitset class, which automatically converts the decimal number to its binary representation. Finally, we print the binary equivalent of the decimal number using the cout statement.
Note that the size of the bitset object can be modified by changing the template parameter in the declaration of binary to a different value. For example, if we want to store the binary representation of a 32-bit integer, we can change the declaration to bitset<32> binary(decimal).
Also explore: Top C++ Interview Questions and Answers for 2023
Endnotes
In conclusion, we have explored various methods of the binary representation of numbers. We have converted numbers between binary and decimal formats in C++. Each method has its own advantages and disadvantages, and programmers can choose the most appropriate one based on their specific needs and preferences. Understanding how to convert numbers between binary and decimal formats is a fundamental skill in computer programming and is essential for working with low-level data. By mastering these methods, programmers can improve their ability to manipulate and analyze data more efficiently and effectively.
Explore our C++ articles to learn more about the language and consolidate your knowledge of the fundamentals.
Contributed By: Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio