# 2022-09-03 - DataTypes: Numbers

### DataTypes

- Part 1 - Introduction
- Part 2 - Bits
- Part 3 - Logical Values
- Part 4 - Numbers
- Part 5 - Binary Arithmetic
- Part 6 - Negative Numbers

So far we’ve only used bits to store logical values, true and false, but there’s limited information that can be represented with just true and false. How do you encode that a building has 5 floors? A series of boolean values encoding if the building has floor 1, floor 2, floor 3… ? That’s manageable for a few floors but will get very complicated if you have hundreds of them. Luckily bits or binary-digits as the name suggests can be used to represent binary, base-2, numbers.

To start let’s look at decimal, base-10, numbers which is how we typically think of numbers. If I showed you the number 824 you’d probably think of it as eight-hundred and twenty-four. Each digit position represents a power of 10. You’ve got 1s, 10s, 100s, 1000s etc or using powers of 10 we have 1 = 10^{0}, 10 = 10^{1}, 100 = 10^{2}, 1000 = 10^{3} etc. We can think of 824 then as 8 x 10^{2} + 2 x 10^{1} + 4 x 10^{0}. This is actually how all bases work, the difference is the number you are multiplying the digits by.

Binary numbers are base 2 which means they have 2 digits (0 and 1) and the digit positions are powers of 2. So the first position is 2^{0} = 1, the second is 2^{1} = 2, then we have 2^{2} = 4, 2^{3} = 8, 2^{4}_{} = 16 etc. To convert the binary number 100101 to decimal we can use the same process we used for 824 but with powers of 2 instead of powers of 10. This gives us 1 x 2^{5} + 0 x 2^{4} + 0 x 2^{3} + 1 x 2^{2} + 0 x 2^{1} + 1 x 2^{0 }which is 32 + 4 + 1 or 37 in base-10. Basically you add up the power of 2 for any position that has a 1 in it. The more bits we have the larger the number we can represent. With a byte we can represent numbers for 0 to 255, with 2 bytes we can go up to 65535 and with 4 bytes we can go up to 4,294,967,295.

To convert from decimal to binary you divide the value by 2, keep track of the remainders you get until you end up with 0 and then put the remainders in reverse order. For example 37 / 2 is 18 with a remainder of 1. 18 / 2 is 9 with no remainder. 9 / 2 is 4 with a remainder of 1. 4 / 2 is 2 with no remainder. 2 / 2 = 1 with no remainder. 1 / 2 is 0 with a remainder of 1. Working backwards our remainders are 1, 0, 0, 1, 0, 1 which is the binary version of 37. Each divisions determines if we need a bit set at the current position starting from the right-most bit. The first division determines the zeroth bit, the next division determines the oneth bit etc.

There are two other bases that come up often in computing. Octal, or base 8, which uses the digits 0 to 7 and hexadecimal, or base 16, which uses the digits 0 to 9 and A to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). These bases are used because you can easily convert between them and binary by grouping bits. This gives use a more compact way to represent values without having to do complicated conversions. To convert from binary to octal we use groups of 3 bits and then write the value of those groups. The binary number 101100011010 split into groups of 3 bits would be 101|100|011|010 and if we take the values of those groups we get the octal number 5432. Hexadecimal works the same way but with groups of 4 bits. 101100011010 split into groups of 4 bits would be 1011|0001|1010 and if we take the values of those groups we get the hexadecimal number B1A. To convert back we simply expand the values back into 3 of 4 bit groups depending on our starting base. Converting from these bases to decimal works the same way as with binary but with powers of 8 and powers of 16 respectively.

All the arithmetic operations work the same with base 2 as they do with decimal numbers just with less digits. So instead of 1 + 1 equaling 2 we have 1 + 1 = 0 with a carry leftover to give us 10 which is the same as 2.

Next time we will look at how to represent negative numbers

## Comments: