# Eli5 Binary and hexadecimals

45 views
0

I’m struggling in class with understanding and calculating😭😭 It’s just not clicking for me

In: 6

They’re different ways of counting up.

In “normal” (decimal) counting, we have ten symbols: 0 1 2 3 4 5 6 7 8 9. We start counting up at 0, and when we get to 9, we’re out of symbols, so the system we use is to put a 0 there, put a 1 one space to the left, and then start counting up again. Repeat as long as necessary.

In binary, there’s only two symbols: 1 0. Again we start counting up at 0, but as soon as we hit 1, we’re out of symbols, so we again place our 1 one space to the left, and start counting up again.

In hex, we instead have sixteen symbols: 0 1 2 3 4 5 6 7 8 9 A B C D E F. Now we don’t have to move over one space until we’ve counted up to F, where you start over at 1 again.

As it turns out, using binary is useful for computers, because they’re all based on circuits that have two states: on (1) and off (0). Decimal counting is arbitrary, but works well for beings with five fingers on each hand. Hex is useful for fitting larger numbers in less space, which is why they’re common in computing.

Binary is base two.

1 is 1.

2 is 10.

3 is 11.

4 is 100.

Hexadecimal is base sixteen. Works the same as any other base, but we need letters to go past nine, hence A-F for 10 through 15.

What’s nice between binary and hex is that one hex character is four binary characters mushed together.

0 = 0000

1 = 0001

2 = 0010

3 = 0011

4 = 0100

5 = 0101

6 = 0110

7 = 0111

8 = 1000

9 = 1001

A = 1010

B = 1011

C = 1100

D = 1101

E = 1110

F = 1111

So, any binary number can be made into a hex number with 1/4 the characters.

It’s useful to have a high base around, even if you’re thinking in binary. We’re used to ten, but as you’ve seen converting to and from binary and base ten is annoying. Base sixteen makes the algorithm easy: just count every four binary digits, add them up as if it were just those four digits, and add the value to the hex number as one more hex digit. Going the other way is even easier, take the hex number one character at a time and spit out the 4-digit binary equivalent.

Consider that number places are like little containers, and the containers can only hold [this many] items before getting full up. After one container is full up, we empty it and say “well, that’s one full up container,” and we mark that by putting one item in the second container. Our containers look like this now: 1|0 or (10).

In “normal” counting (that is, base ten, or decimal)
a container can hold no items, or one item, or up to nine items.
When it has nine items, it is full up, and before we can add another item, we empty the container, and mark that by putting one item in the next container, signifying there’s been one full container.

Say you have twentysix beans.
You put nine of them in the right-most container, and when you pick up the 10th bean, you put it in the second-right-most container, and you empty the rightmost container. This is the number 1|0, 10, ten.

You fill the rightmost container with another nine beans, and when you pick up the 10th bean, you put it in the second-rightmost container, which now has two beans, and you empty the rightmost container. This is the number twenty. 2|0 (20)

You add six beans to the rightmost container.
You now have the number twentysix. 2|6 (26)

**Binary** is just like decimal, but the container gets full up with just one bean. When you go to add the second bean, there’s no room in the rightmost container, so you empty it and mark in the secondrightmost container that you’ve had one full up container. Two beans, written in binary, is 1|0 (10). That looks like ten, but it takes a while to get used to, and yeah, it is a little weird.

Add one more bean to total three beans, and you have the binary number 1|1 (11). Two containers that are full up.

Add a fourth bean. You go to add it to the rightmost container, but that too is full up, so you empty that container and go to the secondrightmost container to mark another full up container, but the secondrightmost is full up as well, so you empty it and go to the *next* container. You now have the number 1|0|0 (100), which is four.

They’re both positional number systems–much like the one you’re used to.

Base-10, which is what the normal number system is called, allows for 10 separate characters (0 through 9) to represent 10 separate values.

When the value you’re trying to write down exceeds the maximum value of the position you’re filling, the value rolls over to the next column and the current column reverts to the minimum value.

So, 9 + 1 exceeds the maximum value for one column in a base-10 system. So the value rolls over; 9 becomes 10.

This is true over any number of columns. 99 + 1, for example, goes like this:

99 + 1 = 9 {10, which exceeds the maximum value}, so that rolls over again to {10}0. That 10 also exceeds the maximum value, so it rolls over *again* to 100. This is the basis of “carrying,” as it’s commonly referred to.

You may have seen binary or hex written as exponential values… this is true for base-10 and all other systems, too. The more generic way of writing this is base^position . The “ones” column in a normal number is essentially 10^0 times whatever the value in that column is; the “tens” column is 10^1 times whatever the value in that column is, etc.

So, written like that, “100” becomes “1*10^2 + 0*10^1, + 0*10^0 “. A more complex number, say 2093, would be “2*10^3 + 0*10^2 + 9*10^1 + 3*10^0 “.

As I said, this is true for all bases. In a base-4 system, the maximum value character would be 3, so the (base-10) value “100” would be “1210” in base 4, because that works out to be 1*4^3 + 2*4^2 + 1*4^1 + 0*4^0. You can check that if you want–it works out to be 64 + 32 + 4 + 0 = 100.

So, binary and hexadecimal. These are simply popular bases to work with. Binary is base-2, hexadecimal is base-16.

So, in binary, the base-10 value “100” is just “1100100”. We can do the same math to check this: 1*2^6 + 1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 0*2^0. That works out to be 64 + 32 + 0 + 0 + 4 + 0 + 0 = 100.

Hexadecimal is exactly the same… except that we don’t have number characters to represent values higher than 9, so we use letters. 0 through 9 are the same; 10 is represented by A, 11 by B, and so on. Hexadecimal is base-16, so the characters are 0 through F.

In computers, you may even come across “octal”, which is just base-8.

This may be a bit more middle school than 5, but the thing that helped me a lot when we did different number bases in school was to remember “places”. When we’re first learning to count they make a big deal of One’s, Ten’s, Hundred’s, places. Because the way we learn how to count is using base 10, or decimal, each “place” in a number is a power of 10. (10^0, 10^1, 10^2) But after a few years they stop talking as much about 10’s and 100’s and saying stuff like “3 1’s and 2 10’s” so it’s easy to forget about places.

So we have places and we also have numerals indicating a value multiple. In base 10 we have numerals 0-9 because we need 10 values.

So now we have values and places. We have 10 values because each place represents a multiple of 10, so we need 10 possible values in each place to be able to represent numbers in order without skipping any.

We may have just internalized converting 436 to base 10 as “counting” but we could also learn to convert it like this:

Starting from the right read the value in each place.

Place | Value
———|———-
1|6
10|3
100|4

Multiply the value in that place by the place it’s in and add that result to all previous results:

`(6*1)+(3*10)+(4*100)=6+10+400=436`

Yeah now you know how to read numbers, so what?

Well the same longhand process I described will let you read numbers in _any_ base.

Let’s look a hex value like: 1B4

Places in hex are 1’s (16^0), 16’s (16^1), 256’s (16^2)

Place | Value
———|———-
1|4
16|B
256|1

And now the math:

`(1*4)+(16*B)+(256*1)=4+176+256=436`

Same number.

Note that B gets treated as if it was 11 (base 10) because after 0-9 we just keep going with letters using increasing decimal values so A=10, B=11, etc.

Binary is the same method except your places are based on powers of 2. 2^0 = 1, 2^1 = 2, 2^2 = 4.

Edit:

Thinking in places also lets you relearn how to do calculations directly in bases. Like if you remember adding decimals you add the 1’s together, then carry the 1 to the 10’s place, then carry the 10 to the 100’s place as you add numbers that overflow the “space” they’re in.

In hex `1B4+10=1C4`because you just add the places together.

Place | Left side | Right side | Result
———|———-|——–|-
1|4|0|4
16|B|1|C
256|1|0|1

Or here’s one that needs to carry a number to the next place

Place | Left side | Right side | Result
———|———-|——–|-
1|4|C|0 (C=12, 4+12=16 which is larger than a single numeral in hex can express)
16|B|1|C (Carry the 1) = D
256|1|0|1

Or 1B4+1C=1D0.