Binary Number System (Intro, Conversions and Operations)
Published: 2023-09-13 19:00:13

Binary numbers are the foundational language of computers, serving as the backbone for all digital operations. Understanding binary numbers, their conversions to other number systems, handling negative binary numbers, and performing mathematical operations with them is crucial in computer science. In this blog post, we'll dive into the world of binary numbers to understand these fundamental concepts.
Binary numbers, also known as base-2 numbers, are composed of only two digits: 0 and 1. Like our decimal system (base-10), where each digit represents a power of 10, in binary, each digit represents a power of 2. Let's look at a simple example:
Our decimal number 101 is the same as 1100101 in binary. 
In the binary system, each position holds a power of 2, starting from the right with 2^0, then 2^1, 2^2, and so on. To convert decimal to binary, you repeatedly divide the decimal number by 2 and note the remainder from each division. Reading the remainders from bottom to top gives you the binary representation.
## Conversions to Other Number Systems
While binary is the native language of computers, it's often necessary to convert between different number systems, like decimal (base-10), octal (base-8), and hexadecimal (base-16). Here we gonna have a short look on these conversions:
**Binary to Decimal**: To convert binary to decimal, multiply each binary digit by the corresponding power of 2 and sum the results. Let's convert the binary number "1101" to decimal.
Start from the right and assign powers of 2: 2^0, 2^1, 2^2, 2^3.
Multiply each binary digit by its corresponding power of 2:
1 * 2^0 = 1
0 * 2^1 = 0
1 * 2^2 = 4
1 * 2^3 = 8
Add these results together: 1 + 0 + 4 + 8 = 13. So, "1101" in binary is equivalent to "13" in decimal.
**Decimal to Binary**: As I mentioned earlier, repeatedly divide the decimal number by 2, recording the remainder. Reading the remainders from bottom to top gives you the binary representation. Here's an example how we can convert 26 in decimal to binary. 
Start by repeatedly dividing 26 by 2 and keeping track of remainders.
26 / 2 = 13 remainder 0
13 / 2 = 6 remainder 1
6 / 2 = 3 remainder 0
3 / 2 = 1 remainder 1
1 / 2 = 0 remainder 1
Now, read the remainders from bottom to top: "11010."
So, "26" in decimal is equivalent to "11010" in binary.
**Binary to Octal or Hexadecimal**: To convert binary numbers to Octal (base-8) or Hexadecimal (base-16), we group binary digits into sets of three (for octal) or four (for hexadecimal) from right to left and convert each group to the corresponding base. After that we combine the results.
For example this is how we convert the binary number "1101011" to octal.
Group the binary digits into sets of three, starting from the right: "110" and "101" and "1."
Convert each group to its octal equivalent:
"110" in binary is "6" in octal.
"101" in binary is "5" in octal.
"1" in binary is "1" in octal.
Combine the octal values: "651."
So, "1101011" in binary is equivalent to "651" in octal.
We divide digits into groups of three for base-8 and into groups of four for hexadecimal, because 8=2^3 and 16=2^4. I believe the pattern is evident. 
**Octal/Hexadecimal to Binary:** So how do we do the reverse conversion ? Start by grouping each octal or hexadecimal digit into its binary equivalent. Each octal digit corresponds to a group of three binary digits and each hexadecimal digit corresponds to a group of four binary digits. After converting each octal or hexadecimal digit, combine the binary representations together to form the binary equivalent of the original octal/hexadecimal number.
**Negative Binary Numbers**
Representing negative numbers in binary involves using a system called Two's Complement. In Two's Complement, the leftmost bit (most significant bit) represents the sign, where 0 indicates positive and 1 indicates negative. To negate a binary number, you invert (flip) all its bits (change 0s to 1s and vice versa) and add 1 to the result. Let's take an example with a "-5" in decimal.
Positive Binary Representation of 5: "0101".
Then we change all "0s" to "1s" and all "1s" to "0s." and get inverted Binary: "1010."
And at last we add 1 to "1010" results in "1011."
So, the Two's Complement representation of "-5" is "1011."
**Mathematical Operations with Binary Numbers**
Performing arithmetic operations with binary numbers is similar to decimal arithmetic, with a few differences:
Addition: Add binary digits from right to left, carrying over any carry values to the next column, just like in decimal addition.
Subtraction: Subtraction in binary often involves borrowing. When subtracting, if a digit is smaller in the top number, borrow 1 from the next higher bit.
Multiplication and Division: Binary multiplication is similar to decimal multiplication but with only two digits. The division is also similar but requires subtraction to find the quotient.

Thank you all for reading !!! I tried to upload visuals so it would be more pleasant and illustrative for you to read, but I don't why the website didn't allow me. Down below I will leave some YouTube links for you, so you can try to understand each topic better if you want. Byeeee ! 

Intro to Binary Numbers -  ttps://www.youtube.com/watch?v=sXxwr66Y79Y&pp=ygUOYmluYXJ5IG51bWJlcnM%3D
Converting decimal to binary- https://www.youtube.com/watch?v=rsxT4FfRBaM&pp=ygUOYmluYXJ5IG51bWJlcnM%3D
Addition and Subtraction - https://www.youtube.com/watch?v=C5EkxfNEMjE&pp=ygUOYmluYXJ5IG51bWJlcnM%3D
Negative Binary Numbers - https://www.youtube.com/watch?v=sJXTo3EZoxM&pp=ygUPbmVnYXRpdmUgYmluYXJ5