Skip to main content

Math Basics

Number System

What is a number?

A number is a mathematical object used to count, measure, and label. Numbers are also used to represent quantities and make calculations. For example, we use numbers in everyday life to count money, time, and things.

Numbers themselves are categorized into various types, such as Natural numbers, Whole numbers, Rational numbers, and Irrational numbers.

Numbers are represented using a number system, which is a set of symbols or numerals.

What is the Number system?

Number systems are fundamental to mathematics, serving as the basis for counting, measuring, and performing arithmetic operations. These systems also play a crucial role in computing, as computers use them to represent and process data.

Different number systems have been developed, each with unique properties and applications, including the Binary, Octal, Decimal, and Hexadecimal systems.

In this article, we'll explore the various number systems we commonly use: Binary, Octal, Decimal, and Hexadecimal.
We'll explore how each system works, understand its distinctive characteristics, and learn how to convert numbers between these systems.

Kinds of number systems

There are many different types of number systems in which the four main types are as follows :

  1. Decimal number system (Base 10)
  2. Binary number system (Base 2)
  3. Octal number system (Base 8)
  4. Hexadecimal number system (Base 16)

Before understanding each one of them let's understand what is base of a number is.

In mathematics, the term "base" (or radix) refers to the number of symbols used in a numbering system.
For example, base 10 (decimal) uses ten symbols: 0 through 9.
Base 16 (hexadecimal) uses sixteen symbols: 0 through 9 and A through F.
Base 2 (Binary) uses 2 symbols: 0 and 1.
Base 8 (Octal) uses 8 symbols: 0 and 7.

The base determines how numbers are represented and interpreted in that system.

Let's discuss each one of them in detail :

Decimal Number System ( Base 10 ) :

The decimal number system is based on ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, with a base of 10. This is the most common system we use in everyday life to represent numbers. When a number is written without specifying a base, it is typically assumed to be in the decimal system, with a base of 10.

For instance, 510, 1510, and 152310 are examples of numbers in the decimal system.

Binary Number System ( Base 2 ) :

The binary number system is composed of just two digits: 0 and 1, with a base of 2. In this system, the digits 0 and 1 are referred to as bits, and a group of 8 bits forms a byte. Computers store and process data using bits and bytes, making the binary system fundamental to digital technology.
Unlike the decimal system, the binary system does not include digits like 2, 3, 4, 5, and so on. Computers use base 2 because their hardware operates using electrical signals that are either on or off, perfectly represented by 0 and 1.

For example, the binary number 1012 represents 5 in decimal, 10112 represents 11, and 10012 represents 9.

Octal Number System (Base 8) :

The octal number system is a base of 8 using its digits as follows: 0, 1, 2, 3, 4, 5, 6, and 7. The octal system has fewer digits than other numeral systems which reduces chances of computational errors. The decimal or hexadecimal system differs from the octal one by not including numbers nine and eight.
Like the binary numeral system, the octal system is commonly used in computers especially microcomputers represented with numbers 0 to 7.

For instance, the octal number 358 is 29 in decimal, 238 is 19, and 1418 is 97. The octal system groups binary digits into sets of three, simplifying the representation of binary data, which we will learn further.

Hexadecimal Number System (Base 16) :

The hexadecimal number system includes sixteen unique characters, using the digits 0 through 9 and the letters A to F, representing the values 10 to 15, respectively, where the letters A through F, where A represents 10, B represents 11, C represents 12, D represents 13, E represents 14, and F represents 15.
With a base of 16, hexadecimal is especially useful in computing due to its efficiency in representing large binary numbers in a more compact form.
This system enables quicker and easier conversions and interpretations in digital systems, making it a preferred choice in various computing applications.

For instance, the hexadecimal number 7B416 corresponds to 1972 in decimal, 6E16 is 110, and 4B2A16 is 19210.

Decimal to Binary and vice versa :

Before learning the conversion from decimal to binary and vice versa, let's understand how we represent a decimal number.

For example, the decimal number 152310 is represented as
152310 = represent it as a summing power of 10.

Now let us try to represent it in binary form, so how will we do it?
Let's see!

Decimal to Binary :

To convert from Decimal to Binary we need to change the base of the number from base 10 to base 2. One of the methods to convert the number from base 10 to base 2 is by dividing the given decimal number recursively by 2.
Then, the remainder is noted down till we get 0 as the final quotient. After this step, these remainders are written in reverse order to get the binary value of the given decimal number.

Steps to convert a decimal number into a binary number :

Step 1: Divide the given decimal number by 2 and note down the remainder

Step 2: Repeat the above steps until you get 0 as the quotient.

Step 3: Now, write the remainder in such a way that the last remainder is written first, followed by the rest in the reverse order.

Let's understand it with an example :

Convert 1810 into a binary number.

Now once we have learned about decimal to binary conversion.
Let us understand the conversion from binary to decimal.

Binary to Decimal :

For converting binary to decimal we just have to do the opposite of it that we have done while converting from decimal to binary.

We need to multiply the given binary number starting from the rightmost digit with the exponents of the base and add all of them to get the decimal number.

Why this?
This method works because it accurately reflects the binary system's structure, where each bit's value depends on its position. By converting each bit’s contribution based on its positional power of 2 and summing these values, we obtain the correct decimal representation (base 10) of the binary number.

Now let's understand the steps to convert a Binary number to a decimal number

Step 1: Multiply each digit of the given number, starting from the rightmost digit, with the exponents of the base.

Step 2: The exponents should start with 0 and increase by 1 every time we move from right to left.

Step 3: Simplify each of the above products and add them.

Let us understand it with an example where we need to convert a binary number 100102 into a decimal number.

Decimal to Other Bases

So if we need to change a decimal number to any number with a different base let's say octal (base 8) or hexadecimal (base 16).

We will simply divide that number recursively with the given base and then, the remainder are noted down till we get 0 as the final quotient. After this step, these remainders are written in reverse order to get the octal/hexadecimal value of the given decimal number.

Decimal to Octal :

Steps to convert a Decimal number to an Octal number

Step 1: Check if the decimal number is less than 8. If yes, the octal number is the same. If no, then proceed forward.

For example, take the conversion x to octal

Step 2: Divide the given decimal number by 8 and note down the remainder.

Step 3: Repeat the above steps until we get the quotient to be less than 8.

Step 4: Follow the reverse order pattern to arrange all the values of the remainder

Step 5an: The obtained number is the required octal number.

Let us understand with an example i.e. converting 45010 into an octal number :

Till now you have learned decimal to octal, let say now you want to convert an octal number to a decimal number then what should we do?

We know that an octal number has base 8, We need to multiply the given octal number starting from the rightmost digit with the exponents of the base and add all of them to get the decimal number.

Decimal to Hexadecimal :

Steps to convert a Decimal number to a Hexadecimal number :

Step 1: Check if the decimal number is less than 16. If yes, the hexadecimal number is the same. If no, then proceed forward.

Step 2: Divide the given decimal number system value by 16 and note the remainder.

Step 3: Divide the quotient by 16. Repeat this until you get a quotient equal to zero.

Step 4: Use the characters A, B, C, D, E, and F in place of 10, 11, 12, 13, 14, and 15 in the remainder respectively, wherever needed.

Step 5: Follow the reverse order pattern to arrange all the values of the remainder.

Step 6: The obtained number is the required hexadecimal number.

Let us understand with an example i.e. converting 45010 into a Hexadecimal number and vice versa:

Now you have learned decimal to hexadecimal, let's say you want to convert a Hexadecimal number to a decimal number then what should we do?

We know that a hexadecimal number has a base of 16, We need to multiply the given octal number starting from the rightmost digit with the exponents of the base and add all of them to get the decimal number.

Conversion between Number System Using Groupings

Binary to Octal :

To convert a number from the binary system to the octal system, we first need to understand what these systems are and how they are correlated.

Understanding Number Systems

  1. Binary System: The binary system uses only two digits: 0 and 1. Each digit's position represents a power of 2. For example, in the binary number 1011, the rightmost digit represents 2^0 ie 1, the next 2^1 ie 2, and so on.
  2. Octal System: The octal system uses eight digits: 0 through 7.
    Each digit's position represents a power of 8. For instance, in the octal
    number 12, the rightmost digit represents 8^0 .i.e 1, and the next represents 8^1 .i.e 8.

Now since 8 is 2^3, every octal digit corresponds exactly to three binary digits.
This means that when converting from binary to octal, we can effectively group the binary digits into sets of three, allowing for a direct mapping to octal digits.

Steps to Convert Binary to Octal

Here’s a detailed, step-by-step guide on how to perform the conversion:

Step 1: Identify the Binary Number
  • Ensure your number is binary, composed only of 0s and 1s. For example, 110101 is a valid binary number.
Step 2: Group the Binary Digits
  • Starting from the rightmost digit, divide the binary number into groups of three. For instance, if you have 110101, you group it as 1 101 01.
Step 3: Add Leading Zeros if Necessary
  • If the leftmost group has fewer than three digits, pad it with leading zeros. For example, 110101 would become 001 101 101 to ensure all groups contain three digits.
Step 4: Convert Each Group to an Octal
  • Use a binary to octal conversion table to translate each group of three binary digits into a single octal digit. Here’s a simple reference:
Binary Octal
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

For example, if you have the groups 001, 101, and 101, you convert them as follows:

  • 001 → 1
  • 101 → 5
  • 101 → 5
Step 5: Combine the Octal Digits
  • Finally, concatenate the octal digits from each group to form the complete octal number. From our previous example, the binary number 110101 converts to octal as 155.

Final Example

Let’s summarize the process with a complete example:

  1. Binary Number: 1011101
  2. Grouping: From right, it groups to 1 011 101. Add leading zeros: 001 011 101.
  3. Convert:
    • 001 → 1
    • 011 → 3
    • 101 → 5
  4. Combine: The final octal number is 135.

Binary to Hexadecimal :

To convert a number from the binary system to the hexadecimal system, we first need to understand what these systems are and how they are related.

Understanding Number Systems

  1. Binary System: The binary system uses only two digits: 0 and 1. Each digit's position represents a power of 2. For example, in the binary number 1011, the rightmost digit represents 2^0 ie 1, the next 2^1 ie 2, and so on.
  2. Hexadecimal System: The hexadecimal system uses sixteen digits: 0 through 9 and A through F (where A=10, B=11, C=12, D=13, E=14, F=15).
    Each position in a hexadecimal number represents a power of 16.
    For instance, in the hexadecimal number 2F, the rightmost digit represents 16^0 .i.e 1 and the next digit represents 16^1 .i.e 16.

Now since 16 is 2^4, each hexadecimal digit corresponds exactly to four binary digits. This means that when converting from binary to hexadecimal, we can group the binary digits into sets of four, allowing for a direct mapping to hexadecimal digits.

Steps to Convert Binary to Hexadecimal

Here’s a detailed, step-by-step guide on how to perform the conversion:

Step 1: Identify the Binary Number
  • Ensure your number is binary, consisting only of 0s and 1s. For example, 110101 is a valid binary number.
Step 2: Group the Binary Digits
  • Starting from the rightmost digit, divide the binary number into groups of four. For instance, if you have 110101, you would group it as 11 0101.
Step 3: Add Leading Zeros if Necessary
  • If the leftmost group has fewer than four digits, pad it with leading zeros. For example, 110101 becomes 0011 0101 to ensure each group contains four digits.
Step 4: Convert Each Group to Hexadecimal
  • Use a binary-to-hexadecimal conversion table to translate each group of four binary digits into a single hexadecimal digit.

Here’s a simple reference:

Binary Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

For example, if you have the groups 0011 and 0101, you convert them as follows:

  • 0011 → 3
  • 0101 → 5
Step 5: Combine the Hexadecimal Digits
  • Finally, concatenate the hexadecimal digits from each group to form the complete hexadecimal number.
    From our previous example, the binary number 110101 converts to hexadecimal as 35.

Final Example

Let’s summarize the process with a complete example:

  1. Binary Number: 10111011
  2. Grouping: From the right, it groups to 1011 1011.
  3. Convert:
    • 1011 → B
    • 1011 → B
  4. Combine: The final hexadecimal number is BB.

Other Bases Conversion

Now if we want to transform from any base let's say base1 to any other base let's say base2 we can simply change that base1 to binary/decimal and then we can apply some operations to change it to base2.

Addition and subtraction in binary vs decimal

Addition and Subtraction in Decimal :

Addition and subtraction in decimal is simple math that we apply in our day-to-day lives.

Let's suppose we want to add two numbers i.e. 7 and 5, on addition it gives us 12.

Similarly, if we want to subtract two numbers i.e. if we want to subtract 5 from 7 we get 2.

Addition and Subtraction of Binary Numbers :

Binary addition and subtraction are fundamental operations in computer science, similar to decimal addition and subtraction but with base 2 (binary) instead of base 10 (decimal).

Binary Addition

Binary addition follows the same principles as decimal addition but with only
two digits (0 and 1).

Rules for Binary Addition:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (This results in a sum of 0 with a carry of 1)

Let's add two binary numbers 10112 and 10012 :

Binary Subtraction

Binary subtraction also follows similar rules as decimal subtraction.

Rules for Binary Subtraction:
0 - 0 = 0
1 - 1 = 0
1 - 0 = 1
0 - 1 = 1 with a borrow of 1 (This is equivalent to 102​ - 12)

Let's subtract two binary numbers 10112 and 10012 :

Gray code

Gray code, also known as Gray binary code, is a binary numeral system where two successive values differ in only one bit. This property is often useful in applications where errors due to noise or other factors can cause multiple bits to change simultaneously.

Applications of Gray code:

-> Digital circuits
-> Analog-to-digital converters (ADCs)
-> Rotary encoders
-> Error detection and correction

Binary to gray code and vice versa

Converting Binary to Gray Code :

Binary to Gray conversion: The Most Significant Bit (MSB) of the Gray code is always the same as the MSB of the corresponding binary code. The other bits of the Gray code can be obtained by performing an XOR operation between each bit of the binary code and the bit immediately preceding it.

Converting Gray Code to Binary: The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given Gray code. The other bits of the binary code can be obtained by examining the Gray code bits at each index. If the current Gray code bit is 0, copy the previous binary code bit; if the current Gray code bit is 1, use the inverse (complement) of the previous binary code bit.

Signed and unsigned number


In programming, particularly in languages like C, C++, and Java, the terms "signed" and "unsigned" refer to the way numbers are stored and represented in memory.

Unsigned:

Unsigned numbers can only represent non-negative values (i.e., zero and positive numbers).

All n bits are used to represent the magnitude of the number, which allows for a greater positive range but no negative numbers.

For example
1610 is represented as (10000)2, where all n bits are used to represent the number.

Example in C/C++ :

unsigned int unsignedVar = 42;

Signed:

Signed numbers can represent both positive and negative values.

For a bit binary number, the MSB(sign bit) is used to represent whether the number is positive or negative, and the remaining bits are used to represent the magnitude of a number. If the sign is 0, the number is positive if it's 1, the number is negative.

In a signed 8-bit integer, you can represent numbers from -128 to 127.

Let us understand signed number with the below image,

But wait we have an ambiguity, what sign should 0 have?

we represent 0 as +0 and -0 too,
so for n = 4 bit where 1 bit (MSB) is sign bit remaining 3 bit is used to represent the magnitude we can write it as,
+0 could be represented as 0 0 0 0
- 0 could be represented as 1 0 0 0

Example in C/C++:

int signedVar = -42;

1s and 2s complement

Introduction

In computer systems, negative numbers are represented using different techniques to distinguish them from positive numbers. Two common methods are 1's complement and 2's complement. These methods allow computers to perform arithmetic operations on both positive and negative numbers efficiently.

1's Complement :

For an n-bit binary number whose decimal representation is N,
we can find its 1's complement as (2^n - 1) - N.

Let us understand that a decimal number 1210 consists of 5 bits where
sign bit (MSB) represents the sign of a number and the rest represents the magnitude of a number so we can find its 1's complement as (2^5 - 1) - 12.

For an n-bit number which can be represented as 1's complement form in the range from - (2^(n-1) - 1 ) to (2^(n-1) - 1 ).
For a 4-bit number, we can represent its 1's complement form in a range from
- (2^(4-1) - 1) to (2^(4-1) - 1) = > -15 to 15.

To find the 1's complement of a binary number, simply invert all the bits (change 0's to 1's and 1's to 0's).

For example, the 1's complement of the binary number 0101 is 1010.

This method uses a sign bit (0 for positive, 1 for negative) followed by the magnitude of the number in binary.

However, 1's complement has a drawback:
It has two representations for zero (000...0 and 111...1).

2's Complement

2's complement is a method used to represent negative numbers in binary format.

For a n-bit binary number whose decimal representation is N,
we can find its 2's complement as 2^n - N.
i.e. we can represent it like (2^n - 1) - N + 1.
i.e. 1's complement of binary number + 1.

Let us understand that a decimal number 1210 consists of 5 bits where
sign bit (MSB) represents the sign of a number and the rest represents the magnitude of a number so we can find its 2's complement as 2^5 - 12.

For an n-bit number which can be represented as 2's complement form in a range from - 2^(n-1) to 2^(n-1) - 1.
For a 4-bit number, we can represent its 1's complement form in a range from
- 2^(4-1) to 2^(4-1) - 1 = > -8 to 7.

In General the 2's complement of a binary number :

Find it's 1's complement: Change all 0s to 1s and 1s to 0s (similar to 1's complement).
Add 1: Add 1 to the result obtained in step 1.

For example, the 2's complement of the binary number 0101 is 1011.

Unlike 1's complement, 2's complement has only one representation for zero (000...0). This makes arithmetic operations with 2's complement more straightforward and efficient.

7s and 9s complement

7's Complement ( Octal Number System )

7's complement is a method used to represent negative numbers in base-8 (octal) format.

To find the 7's complement of an octal number, you simply subtract each digit from 7.

For example, to find the 7's complement of the octal number 123:

Subtract each digit from 7: 7 - 1 = 6, 7 - 2 = 5, 7 - 3 = 4.
The 7's complement of 123 is 654.

Similar to 1's complement, 7's complement can be used to represent negative numbers in octal arithmetic.

So now if we want to find the 8's complement of that number we could simply do like 7's complement of that number + 1.

9's Complement ( Decimal Number System )

9's complement is a method used to represent negative numbers in base-10 (decimal) format.

To find the 9's complement of a decimal number, you subtract each digit from 9.

For example, to find the 9's complement of the decimal number 123:

Subtract each digit from 9: 9 - 1 = 8, 9 - 2 = 7, 9 - 3 = 6.
The 9's complement of 123 is 876.

So Now if we want to find the 10's complement of that number we could simply do like 9's complement of that number + 1.

ASCII and UNICODE

ASCII

ASCII ( American Standard Code for Information Interchange )
It was developed in the early 1960s as a standard character set to ensure consistent encoding and communication between different devices and platforms.

ASCII is a 7-bit character set, meaning it can represent 128 unique characters.

These include :
Control Characters : 0–31 and 127 (non-printable, used for control purposes in text processing).

Printable Characters: 32–126, which include:
Numbers : 0–9
Uppercase Letters: A–Z
Lowercase Letters: a–z
Punctuation and Special Symbols: Characters like !, @, #, etc.

Image for ASCII table

UNICODE

Unicode is a global standard designed to represent text characters from virtually all writing systems of the world. It provides a unique numerical code for each character, allowing computers to store, process, and consistently exchange text, regardless of the language or script.

Unicode Consortium :

The Unicode Consortium, a non-profit organization, is responsible for developing and maintaining the Unicode Standard. It brings together experts from various industries to ensure the standard's continued evolution and global adoption.

Applications of Unicode:

Web Development: Unicode is essential for creating websites that support multiple languages and display text correctly in different scripts.

Software Localization: Unicode enables software applications to be easily localized for different languages and regions.

Text Processing and Analysis: Unicode provides a foundation for natural language processing, text mining, and other text-related tasks.

Database Management: Unicode is widely supported by database systems for storing and managing text data in various languages.

Unicode vs. ASCII :

While ASCII is a legacy character encoding standard that supports only a limited subset of characters, Unicode is a much more comprehensive and versatile standard that encompasses a vast array of characters from different writing systems.

Unicode is backward-compatible with ASCII, meaning ASCII characters are represented using the same code points in Unicode.