Binary Conversions

I am a recent graduate at the beginning of my software development career. I enjoy documenting my learnings through my blogs
Binary is a base 2 number system and is made up of 0s and 1s. In this blog post, I will look at converting Binary to Decimal and Hexadecimal numbers. I will also look at converting Binary fractions into Decimals, and Binary addition and subtraction. Lastly, I will cover 2's Complement.
Binary to Decimal
Here is a handy table to help with calculations:

When converting a binary number to a decimal number, you use powers of 2 because binary is a base-2 number system. In a base-2 system, each digit (bit) can have one of two values: 0 or 1. By using powers of 2, you can represent any decimal number by combining these binary digits.
Here's how it works:
The rightmost digit in a binary number represents 2^0 (2 raised to the power of 0), which is equal to 1. So, if you have a 1 in the rightmost position, it contributes 1 to the decimal value. If it's 0, it contributes 0 to the decimal value.
The next digit to the left represents 2^1 (2 raised to the power of 1), which is equal to 2. So, if you have a 1 in this position, it contributes 2 to the decimal value. If it's 0, it contributes 0.
The next digit to the left represents 2^2 (2 raised to the power of 2), which is equal to 4. So, if you have a 1 in this position, it contributes 4 to the decimal value. If it's 0, it contributes 0.
This pattern continues for each position to the left. Each position represents a power of 2 that is one greater than the position to its right. Therefore, you add up the values contributed by each position based on whether it's a 1 or a 0 to get the decimal equivalent of the binary number.
Examples:

Binary Fractions
An example of a binary fraction is 1100.101
To convert this number to a decimal you use 2 to the power of a minus, meaning anything before the decimal point is positive (2^3) and anything after the decimal point is negative (2^-1)

Similar to the table used for the positive powers, here is a table for the negative
Examples:

Binary to Hexadecimal
Group into Sets of Four: Begin by separating the binary number into sets of four digits, starting from the right. If the leftmost group has fewer than four digits, add leading zeros to make it a complete group of four (e.g., 1101 becomes 0011 0101). This step ensures that each group corresponds to a single hexadecimal digit.
Convert Each Group to Decimal: Now, convert each group of four binary digits into its decimal equivalent. You can do this by multiplying each digit by 2 raised to the power of its position from right to left (starting with 0 for the rightmost digit) and then summing up these values. For example, to convert 1101 to decimal:
- 1 * 2^3 + 1 * 2^2 + 0 \ 2^1 + 1 \ 2^0 = 8 + 4 + 0 + 1 = 13
So, the first group, 1101, is equivalent to the decimal value 13.
- Convert Decimal Values to Hexadecimal: Finally, convert the decimal values obtained in step 2 into their hexadecimal equivalents. In hexadecimal, digits range from 0 to 9 and then from A to F. So, for the decimal value 13, its hexadecimal equivalent is D.
Repeat steps 2 and 3 for each group of four binary digits, and then combine the hexadecimal equivalents to get the final hexadecimal representation of the binary number.
For example, let's convert the binary number 11010101 to hexadecimal:
Group into sets of four: 1101 0101
Convert each group to decimal:
1101 (in binary) = 13 (in decimal)
0101 (in binary) = 5 (in decimal)
Convert decimal values to hexadecimal:
13 (in decimal) = D (in hexadecimal)
5 (in decimal) = 5 (in hexadecimal)
Combine the results: 11010101 (binary) = D5 (hexadecimal)
So, 11010101 in binary is equivalent to D5 in hexadecimal.
Examples:



Binary Addition
Adding Binary numbers can be quite simple once you have a bit of practice. It is slightly different to simple addition of a decimal number. There are some things to note.
0 + 0 = 0
1 + 0 = 1
1 + 1 = 10
Normal addition would mean 1 + 1 = 2, however when adding Binary numbers this means it has a decimal equivalent of 2.
You must carry over the 1, leaving the first number as 0
1 + 1 + 1 = 11
Similar to the example above, this example is calculated in two parts:
- 1 + 1 = 0 and you carry over a 1, but in this case, still have an original 1 and now a 0, so 1 + 0 is then calculated giving you a 1.
In most cases you will have a longer binary number, add these numbers column by column (see Ex. 1).
If you want to check your answers are correct, this can be done by converting the numbers to decimal and adding these.
Examples:

Binary Subtraction
Similar to adding binary numbers, subtraction is again simple once you understand a few things
1 - 0 = 1
1 - 1 = 0
0 - 1 = ?
- This equation is usually paired with a longer binary number such as the example below:

- A number ahead is cancelled where possible to add to the 0 value, so when a is cancelled, a 2 is carried over. Then the equation would read 2 - 1 = 1 or in some cases if the 2 is cancelled and carried over again 1 - 1 = 0
Examples:

2's Complement
2's complement is a method used for representing negative numbers in binary and performing binary subtraction. Here are the steps:
Start with the positive binary representation of the number you want to subtract from.
To find the 2's complement of the number you want to subtract (the negative number), invert all the bits (change 1s to 0s and 0s to 1s).
Add 1 to the result of the inversion.
Now, add the positive binary number and the 2's complement of the negative number using normal binary addition.
If there is a carry-out from the most significant bit (leftmost bit), it indicates overflow, and you can ignore it for most arithmetic operations or handle it as needed.
Examples:



This process allows you to perform binary subtraction by effectively adding the negative number's 2's complement to the positive number. It simplifies the hardware required for subtraction in computer systems and ensures consistent arithmetic operations for both positive and negative numbers.




