Binary Number System in C++: A Beginner-Friendly Guide​

binary number system 

Welcome to this comprehensive guide on the binary number system! Whether you're new to programming or just looking to solidify your understanding, this tutorial will break down the binary system into digestible parts, complete with examples and visual aids to enhance your learning experience.​


🔢 What Is the Binary Number System?

The binary number system is a method of representing numbers using only two digits: 0 and 1. This is known as a base-2 system. In contrast, the decimal system, which we use in everyday life, is a base-10 system utilizing digits from 0 to 9.

Computers operate using binary because their fundamental components, transistors, have two states: on (1) and off (0). This makes binary the most efficient way for computers to process and store data.


🔄 Converting Decimal to Binary

To convert a decimal number to binary, follow these steps:​

  1. Divide the decimal number by 2.

  2. Record the remainder (0 or 1).

  3. Update the decimal number to the quotient obtained.

  4. Repeat steps 1–3 until the quotient is 0.

  5. Read the remainders in reverse order to get the binary equivalent.

🧮 Example: Convert 42 to Binary

Let's convert the decimal number 42 to binary:​

Division Step                Quotient                Remainder
42 ÷ 2210
21 ÷ 2                            10                            1
10 ÷ 250
5 ÷ 221
2 ÷ 210
1 ÷ 201

Reading the remainders from bottom to top: 101010

So, 42 in decimal is 101010 in binary.


💻 C++ Code: Decimal to Binary Conversion

Here's how you can implement the conversion in C++:


#include <iostream> using namespace std; void decimalToBinary(int n) { string binary = ""; while (n > 0) { binary = to_string(n % 2) + binary; n /= 2; } cout << "Binary: " << binary << endl; } int main() { int number = 42; decimalToBinary(number); return 0; }


🔁 Converting Binary to Decimal

To convert a binary number to decimal:

  1. Write down the binary number.

  2. Assign powers of 2 to each digit, starting from the right (least significant bit).

  3. Multiply each binary digit by its corresponding power of 2.

  4. Sum all the results to get the decimal equivalent.

🧮 Example: Convert 101010 to Decimal

Let's convert binary 101010 to decimal:

Binary Digit                Power of 2                        Calculation
12⁵ = 321 × 32 = 32
0                                2⁴ = 160 × 16 = 0
12³ = 8                                1 × 8 = 8
02² = 40 × 4 = 0
12¹ = 21 × 2 = 2
02⁰ = 10 × 1 = 0

Total = 32 + 0 + 8 + 0 + 2 + 0 = 42

So, 101010 in binary is 42 in decimal.


💻 C++ Code: Binary to Decimal Conversion

Here's how you can implement the conversion in C++:


#include <iostream> using namespace std; int binaryToDecimal(int n) { int decimal = 0, base = 1; while (n > 0) { int last_digit = n % 10; decimal += last_digit * base; base *= 2; n /= 10; } return decimal; } int main() { int binaryNumber = 101010; cout << "Decimal: " << binaryToDecimal(binaryNumber) << endl; return 0; }


🧠 Memorizing Common Binary Numbers

Familiarize yourself with these common decimal-to-binary conversions:

Decimal        Binary
00
1                    1
2                    10
311
4100
5101
6110
7111
81000
91001
101010

➖ Understanding Two's Complement

Two's complement is a method for representing negative numbers in binary. Here's how it works:

  1. Write the positive binary number.

  2. Invert the digits (change 0 to 1 and 1 to 0).

  3. Add 1 to the inverted number.

🧮 Example: Represent -10 in Two's Complement (8-bit)

  1. Positive 10 in binary (8-bit): 00001010

  2. Invert digits: 11110101

  3. Add 1: 11110101 + 1 = 11110110

So, -10 is represented as 11110110 in two's complement

Comments