0️⃣

Binary to Decimal and Vice Versa

 

 
Binary and decimal are two number systems commonly used in mathematics and computing.
Decimal: Decimal is the base-10 number system that most people are familiar with. It uses ten digits from 0 to 9. Each digit's position in a decimal number represents a power of 10. For example, the number 4567 in decimal notation can be interpreted as (4 x 10^3) + (5 x 10^2) + (6 x 10^1) + (7 x 10^0), which equals 4000 + 500 + 60 + 7, or simply 4567.
Binary: Binary is the base-2 number system used in computing and digital electronics. It consists of only two digits, 0 and 1. Each digit's position in a binary number represents a power of 2. For example, the binary number 1011 can be interpreted as (1 x 2^3) + (0 x 2^2) + (1 x 2^1) + (1 x 2^0), which equals 8 + 0 + 2 + 1, or simply 11 in decimal notation.
In computing, binary numbers are often used to represent data and perform operations at the most fundamental level, as computers are based on binary logic. Binary is also used to represent and manipulate binary-coded information, such as binary files, machine code, and digital signals.
Conversion between Decimal and Binary: Converting a decimal number to binary involves repeatedly dividing the decimal number by 2 and recording the remainders. The binary representation is obtained by reading the remainders in reverse order. For example, the decimal number 25 can be converted to binary as follows:
25 divided by 2 equals 12 with a remainder of 1. 12 divided by 2 equals 6 with a remainder of 0. 6 divided by 2 equals 3 with a remainder of 0. 3 divided by 2 equals 1 with a remainder of 1. 1 divided by 2 equals 0 with a remainder of 1. Reading the remainders in reverse order gives us the binary representation: 11001.
Converting a binary number to decimal involves multiplying each binary digit by the corresponding power of 2 and adding the results. For example, the binary number 11001 can be converted to decimal as follows:
(1 x 2^4) + (1 x 2^3) + (0 x 2^2) + (0 x 2^1) + (1 x 2^0) equals 16 + 8 + 0 + 0 + 1, which is equal to 25 in decimal notation.
These conversions allow us to work with numbers in different number systems depending on the context and requirements of the problem at hand.
 
 

Binary to Decimal

#include <iostream> #include <cmath> using namespace std; int binaryToDecimal(long long binaryNum) { int decimalNum = 0, i = 0, remainder; while (binaryNum != 0) { remainder = binaryNum % 10; binaryNum /= 10; decimalNum += remainder * pow(2, i); ++i; } return decimalNum; } int main() { long long binaryNum; cout << "Enter a binary number: "; cin >> binaryNum; int decimalNum = binaryToDecimal(binaryNum); cout << binaryNum << " in binary = " << decimalNum << " in decimal" << endl; return 0; }
In this code, the binaryToDecimal() function takes a binary number as an argument and returns the decimal equivalent. The function initializes decimalNum to 0, i to 0, and calculates the remainder of the binary number when divided by 10 using modulo operator %. The binary number is then divided by 10 and decimalNum is updated using remainder * pow(2, i), where i is the current position of the binary digit being evaluated. i is then incremented to move to the next binary digit.
The main() function prompts the user to enter a binary number, calls the binaryToDecimal() function, and outputs the results to the console.
 

Decimal to Binary

#include <iostream> #include <vector> using namespace std; long long decimalToBinary(int decimalNum) { long long binaryNum = 0; int bit = 1; while (decimalNum > 0) { binaryNum += (decimalNum % 2) * bit; decimalNum /= 2; bit *= 10; } return binaryNum; } int main() { int decimalNum; cout << "Enter a decimal number: "; cin >> decimalNum; long long binaryNum = decimalToBinary(decimalNum); cout << decimalNum << " in decimal = " << binaryNum << " in binary" << endl; return 0; }
In this code, the decimalToBinary() function takes a decimal number as an argument and returns the binary equivalent. The function initializes binaryNum to 0, bit to 1, and calculates the remainder of the decimal number when divided by 2 using modulo operator %. The decimal number is then divided by 2 and binaryNum is updated using (decimalNum % 2) * bit, where bit is the current position of the binary digit being evaluated. bit is then multiplied by 10 to move to the next binary digit.
The main() function prompts the user to enter a decimal number, calls the decimalToBinary() function, and outputs the results to the console.