# Learn how to program from scratch - 3 (Binary and Hex)

### 1's Self Solutions Oct 11 '17

## Binary

You may have learnt to convert numbers from base 10 to base 2 and vice versa. You probably didn't like doing that but now that you want to be a programmer, it'll be good to just have a fair idea what binary is about.

## Why the need?

### Binary

Programming is the art of writing instructions for a computer. How can you instruct properly if you do not have an idea how a computer thinks and operates? Every single character from A to Z, from 0 to 9 and all symbols, have a binary representation. That's what the computer understands. Each binary digit represents a bit (1 or 0), and that's what computers understand.

You may have to write programs in low level languages that'll demand using binary, hence the need.

### Hexadecimal

Hexadecimal helps to group binary nicely. Binary is base 2, hexadecimal is base 16 (24). Usually, you will see binary digits in groups of 8, which can be converted to two hex digits (as you will see soon).

## Understanding Binary

First of all, forget about converting from base 2 to base 10, and converting from base 10 to 2. Just focus on base 2 and try to understand it the way you were brought up to understand base 10.

1 binary digit is a bit, 8 is a byte and 4 is a nibble. We'll write our binary numbers in 4 digits. Let's count from 0 to 8

0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000.

If you study the digits carefully, you'll notice that, the moment the end of the digit hits 1, it moves to the left with 0's after it. This is similar to hitting 09 and moving to 10 or hitting 099 and moving to 100.

In binary, 2 is our 10. 2 is the number we cannot hit, the moment we are about hitting 2 we must move up. Just like how you can't move from 19 to 110, but rather 19 to 20, in the same way you can't move from 0011 (Which is 3) to 0012 (this isn't binary) but rather 0100 (4).

### 1, 10, 100, 1000 vs 1, 2, 4, 8

In base 10, numbers are categorized under ones, tens, hundreds, thousands etc. This is simply 100, 101, 102, 103 etc. That is to say, 3592 = 3000 + 500 + 90 + 2. With the same idea, binary also goes this way: 20, 21, 22, 23, 24. Which is 0001 (1), 0010 (2), 0100 (4), 1000 (8), 10000(16). In other words, our tens is twos, our hundreds is fours, our thousands is eights.

Now let's count from 0 to 16. Try to understand the pattern with all that you've learnt so far.

0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000.

## Understanding Hexadecimal

Hexadecimal is base 16 which means that, 10 has to be a digit not two, 11 has to be a digit not two, 15 has to be a digit not two, but when we are about to hit 16, then we move up. That is to say, 16 is to 10 in base 10.

Let's count from 0 to 16.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10.

Oops! you weren't expecting alphabets right? Well that's how we represent 10 to 15 in base 16. The thing to note about binary and hex is that, each nibble (4 binary digits) can be converted one hex digit and vice versa. For instance, 00010000 is 24 (16) and it's also 161 hence 10 in hex. In effect, you just converted the first nibble (0001) to hex (1) and the second nibble (0000) to hex (0). You didn't have to work too hard.

Let's try it on another binary number say 00100101. First four gives us 0010 which is 2. Second four gives us 0101 which is 5. So we have 25 in hex. Converting both to base 10 will give us 0 + 0 + 25 + 0 + 0 + 22 + 0 + 20 = 32 + 4 + 1 = 37 and 2x161 + 5x160 = 37.

That means FFFF is 1111 1111 1111 1111 in binary. All you do is pic one hex digit, convert it to 4 binary digits. You don't need to combine the entire number, just break them into fours and each will represent a hex digit.

If you have any questions, leave it as a comment or send it to me through the form at contact.

In our next lesson, we'll look at the physical memory of a computer. Once you have a fair idea how the physical memory works, we can start writing some code. Have fun.