Binary number system
Published: 2023-09-13 16:50:51

Hello, everyone, my name is Davit Hovhannisyan, and today I am going to cover topics related to binary numbers.
Title: Mastering Binary Numbers: Conversion and Arithmetic

Binary numbers are a fundamental concept in mathematics and computer science, serving as the foundation for digital representation and computation. Unlike our everyday decimal system, which relies on ten digits (0-9), binary employs just two digits: 0 and 1. This simple system underpins modern computing and is pivotal in understanding the inner workings of digital devices and data processing.

Binary, often referred to as the base-2 numeral system, employs a positional notation system where each digit, or bit, can be either 0 or 1. These bits convey information in a way similar to how decimal digits (0-9) represent quantities in our daily lives. However, binary is uniquely suited for electronic systems due to its binary states, which align perfectly with the on/off nature of electronic circuits.


The usage of binary numbers is very diverse. There are usages of binary numbers systems mentioned below.

Binary is the core language of computers. All data, instructions, and processes within a computer are represented and manipulated using binary. Every piece of text, image, video, and sound is encoded in binary before being processed by your device's hardware and software.

In electronic circuits, binary digits are used to represent electrical voltage levels. A high voltage (often 1) represents an "on" state, while a low voltage (often 0) signifies an "off" state. This makes it the ideal language for designing and building digital circuits and components like CPUs, memory, and microcontrollers.

Binary is the basis of data storage in various forms, including hard drives, solid-state drives, and optical media. Data is stored as sequences of 0s and 1s, and the manipulation of these binary values allows for data retrieval and storage.

Programmers use binary for tasks like bitwise operations, memory management, and low-level system programming. It provides the fine-grained control necessary for efficient software development.

Binary is used in the encoding of data for transmission over networks and communication channels. This ensures accurate and efficient data transfer between devices.

Many cryptographic algorithms and protocols rely on binary operations to secure data and communications, making binary a vital component of modern cybersecurity.

Now lets understand how to convert binary numbers to decimal and vice versa. Converting between binary and decimal numbers is a fundamental skill when working with digital systems or programming. Here's how you can convert binary to decimal and vice versa:

Start with the binary number you want to convert. For example, let's say you have the binary number "1101."

Assign place values to each digit in the binary number, starting from the right and increasing by a power of 2 for each position. The rightmost digit represents 2^0, the next one to the left represents 2^1, the next represents 2^2, and so on.

For example, in "1101":
Rightmost digit (1): 2^0 = 1
Next digit (0): 2^1 = 2
Next digit (1): 2^2 = 4
Leftmost digit (1): 2^3 = 8

Multiply each digit in the binary number by its corresponding place value and add up the results.

For "1101":

(1 * 8) + (1 * 4) + (0 * 2) + (1 * 1) = 8 + 4 + 0 + 1 = 13

So, the binary number "1101" is equivalent to the decimal number "13."

Now, let's try to understand how to convert decimal numbers to binary. 

Start with the decimal number you want to convert. For example, let's use the decimal number "13."
Divide the decimal number by 2, and keep track of the remainder at each step. Write down the remainder from each division, starting with the remainder of the first division on the right and the remainder of the last division on the left.

13 ÷ 2 = 6 with a remainder of 1 (Write down the remainder: 1)
6 ÷ 2 = 3 with a remainder of 0 (Write down the remainder: 0)
3 ÷ 2 = 1 with a remainder of 1 (Write down the remainder: 1)
1 ÷ 2 = 0 with a remainder of 1 (Write down the remainder: 1)

Once you've completed the divisions, reverse the sequence of remainders you wrote down. This reversed sequence will be the binary representation of the decimal number.

The remainders were 1, 0, 1, 1.
Reverse them to get 1101.

So this is how you can convert decimal number to binary.

Now when we understand what is a binary number and how to convert decimal to binary and the opposite, it is time to dive into binary operations.

First, let's discuss addition.

Binary addition is very similar to decimal addition. You add the digits from right to left, carrying over if the sum is 2 or greater.

Example: Let's add 1101 and 1010.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpw64ot9hodzlvvtnkmu.png)

Start from the rightmost bit (the least significant bit) and add the digits:

1 + 0 = 1
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (Write down 0 and carry over 1)
Continue to the left:

1 (carried over) + 1 + 1 = 11 (Write down 1 and carry over 1)
Finally:

1 (carried over) + 1 = 10 (Write down 0 and carry over 1)
Write down the remaining carry-over (1):


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/933mcx4flwtg1kio8um6.png)

So, 1101 + 1010 in binary is equal to 11011 in binary.

Now let's talk about subtraction.

Binary subtraction is also similar to decimal subtraction. You subtract the digits from right to left, borrowing from higher bits when necessary.

Example: Let's subtract 1010 from 1101.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eqbdptn1ghst3lnk746.png)

Start from the rightmost bit and subtract the digits:

1 - 0 = 1
0 - 1 (needs to borrow) = 1 (borrow 1 from the next bit to the left)
1 - 0 (plus the borrow) = 0
1 - 1 = 0
Write down the results:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zusbwqq42n5146afrwh5.png)

So, 1101 - 1010 in binary is equal to 11 in binary.

It is time to introduce you to multiplication.

Binary multiplication involves shifting and adding, similar to long multiplication in decimal.

Example: Let's multiply 1101 and 101.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovvaibess9uakgpv908q.png)

Start with the rightmost bit of the second number (101) and multiply it by the first number (1101):


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vt6hykh8yosofkr64ie2.png)

Next, shift the second number one position to the left and multiply again:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sffuieacckyns8mi3czl.png)

Repeat the process:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bekmn5djrmv0wrx9k9pw.png)

And finally:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgmldckgi2ln5zoig1l1.png)

So, 1101 multiplied by 101 in binary is equal to 1101000 in binary.

So, when we understand addition, multiplication, and subtraction, it is time to cover division.

Binary division uses long division, similar to decimal division.

Example: Let's divide 1101 by 101.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijved7rbgrja2mr9kz4u.png)

Start with the leftmost bit (1) of the dividend (1101) and divide it by the divisor (101). The result is 1 with a remainder of 1.

Bring down the next bit from the dividend:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9akrzpec0z0nxac06fi.png)

Now, we have 11, which is less than the divisor. Bring down the next bit from the dividend:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqw7bg34e88lsoxfsc9r.png)

Continue the process until there are no more bits in the dividend:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90d3mx0fvwzrils4cz2l.png)

The division is complete, and the final result is 1 with no remainder.
So, 1101 divided by 101 in binary is equal to 1 in binary.

I think it is required to talk about how to write negative binary numbers, so let's discuss it.

Binary numbers can represent both positive and negative values using a system called signed binary representation. There are several methods for representing positive and negative binary numbers, with two's complement being the most common. Here, I'll explain how to represent binary numbers in both positive and negative states using two's complement.

Two's Complement Representation:
Two's complement is a widely used method for representing both positive and negative binary numbers in a way that allows for efficient arithmetic operations. In a two's complement representation:

Positive numbers are represented as usual in binary, with a leading 0.
Negative numbers are represented as the two's complement of their absolute value.
Positive Binary Numbers:
Positive binary numbers are straightforward. You represent them as you would in a regular binary representation, with a leading 0. For example:

Binary representation of +5 is 0101.
Binary representation of +7 is 0111.
Negative Binary Numbers:
To represent negative numbers in two's complement:

Start with the binary representation of the absolute value of the number.
Invert (flip) all the bits (change 0s to 1s and vice versa).
Add 1 to the inverted binary number.
Here's how to represent negative numbers using two's complement:

To represent -5:
Absolute value of 5 is 5, represented as 0101.
Invert the bits: 1010.
Add 1: 1011.
So, the binary representation of -5 in two's complement is 1011.

To represent -7:
Absolute value of 7 is 7, represented as 0111.
Invert the bits: 1000.
Add 1: 1001.
So, the binary representation of -7 in two's complement is 1001.

Range of Representable Values:
In two's complement representation with n bits:

The largest positive value is (2^(n-1) - 1).
The smallest representable negative value is -(2^(n-1)).
For example, in 8-bit two's complement:

Largest positive value: 2^7 - 1 = 127 (01111111 in binary)
Smallest representable negative value: -2^7 = -128 (10000000 in binary)
This system allows for the efficient representation of both positive and negative integers while preserving arithmetic operations like addition, subtraction, multiplication, and division. When you perform operations in two's complement, the result is always consistent with the rules of arithmetic.

Thank you for joining me on this exploration of binary numbers, and I hope you have gained valuable knowledge about this fundamental concept.