Working with different number bases

(Note: These examples use the ^ symbol to mean 'raised to the power of' - this is only for convenience in this document, PureBasic does not currently have a power-of operator! Use the PureBasic command Pow() from the "Math" Library instead.)

Introduction

A number base is a way of representing numbers, using a certain amount of possible symbols per digit. The most common you will know is base 10 (a.k.a. decimal), because there are 10 digits used (0 to 9), and is the one most humans can deal with most easily.

The purpose of this page is to explain different number bases and how you can work with them. The reason for this is that computers work in binary (base 2) and there are some cases where it is advantageous to understand this (for example, when using logical operators, bit masks, etc).

Overview of number bases

Decimal System

Think of a decimal number, and then think of it split into columns. We'll take the example number 1234. Splitting this into columns gives us:
```  1   2   3   4
```
Now think of what the headings for each column are. We know they are units, tens, hundreds and thousands, laid out like this:
```  1000  100   10    1
1    2    3    4
```
We can see that the number 1234 is made up out of
```    1*1000=1000
+ 2* 100= 200
+ 3*  10=  30
+ 4*   1=   4
Total   =1234
```
If we also look at the column headings, you'll see that each time you move one column to the left we multiply by 10, which just so happens to be the number base. Each time you move one column to the right, you divide by 10. The headings of the columns can be called the weights, since to get the total number we need to multiply the digits in each column by the weight. We can express the weights using indices. For example 10^2 means '10 raised to the power of two' or 1*10*10 (=100). Similarly, 10^4 means 1*10*10*10*10 (=10000). Notice the pattern that whatever the index value is, is how many times we multiply the number being raised. 10^0 means 1 (since we multiply by 10 no times). Using negative numbers shows that we need to divide, so for example 10^-2 means 1/10/10 (=0.01). The index values make more sense when we give each column a number - you'll often see things like 'bit 0' which really means 'binary digit in column 0'.
```  In this example, ^ means raised to the power of, so 10^2 means 10 raised to the power of 2.
Column number              3     2     1     0
Weight (index type)     10^3  10^2  10^1  10^0
Weight (actual value)   1000   100    10     1
Example number (1234)      1     2     3     4
```
A few sections ago we showed how to convert the number 1234 into its decimal equivalent. A bit pointless, since it was already in decimal but the general method can be shown from it - so this is how to convert from any number to its decimal value:
```  B = Value of number base

1) Separate the number in whatever base you have into it's columns. For
example, if we had the value 'abcde' in our fictional number base 'B',
the columns would be:   a   b   c   d   e

2) Multiply each symbol by the weight for that column (the weight being
calculated by 'B' raised to the power of the column number):
a * B^4 = a * B * B * B * B
b * B^3 = b * B * B * B
c * B^2 = c * B * B
d * B^1 = d * B
e * B^0 = e

3) Calculate the total of all those values. By writing all those values in their
decimal equivalent during the calculations, it becomes far easier to see the
result and do the calculation (if we are converting into decimal).
```
Converting in the opposite direction (from decimal to the number base 'B') is done using division instead of multiplication:
```  1) Start with the decimal number you want to convert (e.g. 1234).

2) Divide by the target number base ('B') and keep note of the result and the
remainder.

3) Divide the result of (2) by the target number base ('B') and keep note of
the result and the remainder.

4) Keep dividing like this until you end up with a result of 0.

5) Your number in the target number base is the remainders written in the order
most recently calculated to least recent. For example, your number would be
the remainders of the steps in this order 432.
```
More specific examples will be given in the sections about the specific number bases.

Binary System

Everything in a computer is stored in binary (base 2, so giving symbols of '0' or '1') but working with binary numbers follows the same rules as decimal. Each symbol in a binary number is called a bit, short for binary digit. Generally, you will be working with bytes (8-bit), words (16-bit) or longwords (32-bit) as these are the default sizes of PureBasic's built in types. The weights for a byte are shown:
```  (^ means 'raised to the power of', number base is 2 for binary)
Bit/column number         7     6     5     4     3     2     1     0
Weight (index)          2^7   2^6   2^5   2^4   2^3   2^2   2^1   2^0
Weight (actual value)   128    64    32    16     8     4     2     1
```
So, for example, if we had the number 00110011 (Base 2), we could work out the value of it like this:
```    0 * 128
+ 0 *  64
+ 1 *  32
+ 1 *  16
+ 0 *   8
+ 0 *   4
+ 1 *   2
+ 1 *   1
=      51
```
An example of converting back would be if we wanted to write the value 69 in binary. We would do it like this:
```  69 / 2 = 34 r 1     ^
34 / 2 = 17 r 0    /|\
17 / 2 =  8 r 1     |
8 / 2 =  4 r 0     |    Read remainders in this direction
4 / 2 =  2 r 0     |
2 / 2 =  1 r 0     |
1 / 2 =  0 r 1     |
(Stop here since the result of the last divide was 0)

Read the remainders backwards to get the value in binary = 1000101
```
Another thing to consider when working with binary numbers is the representation of negative numbers. In everyday use, we would simply put a negative symbol in front of the decimal number. We cannot do this in binary, but there is a way (after all, PureBasic works mainly with signed numbers, and so must be able to handle negative numbers). This method is called 'twos complement' and apart from all the good features this method has (and won't be explained here, to save some confusion) the simplest way to think of it is the weight of the most significant bit (the MSb is the bit number with the highest weight, in the case of a byte, it would be bit 7) is actually a negative value. So for a two's complement system, the bit weights are changed to:
```  (^ means 'raised to the power of', number base is 2 for binary)
Bit/column number         7     6     5     4     3     2     1     0
Weight (index)         -2^7   2^6   2^5   2^4   2^3   2^2   2^1   2^0
Weight (actual value)  -128    64    32    16     8     4     2     1
```
and you would do the conversion from binary to decimal in exactly the same way as above, but using the new set of weights. So, for example, the number 10000000 (Base 2) is -128, and 10101010 (Base 2) is -86.

To convert from a positive binary number to a negative binary number and vice versa, you invert all the bits and then add 1. For example, 00100010 would be made negative by inverting -> 11011101 and adding 1 -> 11011110.

This makes converting from decimal to binary easier, as you can convert negative decimal numbers as their positive equivalents (using the method shown above) and then make the binary number negative at the end.

Binary numbers are written in PureBasic with a percent symbol in front of them, and obviously all the bits in the number must be a '0' or a '1'. For example, you could use the value %110011 in PureBasic to mean 51. Note that you do not need to put in the leading '0's (that number would really be %00110011) but it might help the readability of your source if you put in the full amount of bits.

Hexadecimal (for base 16, symbols '0'-'9' then 'A'-'F') is a number base which is most commonly used when dealing with computers, as it is probably the easiest of the non-base 10 number bases for humans to understand, and you do not end up with long strings of symbols for your numbers (as you get when working in binary).

Hexadecimal mathematics follows the same rules as with decimal, although you now have 16 symbols per column until you require a carry/borrow. Conversion between hexadecimal and decimal follows the same patterns as between binary and decimal, except the weights are in multiples of 16 and divides are done by 16 instead of 2:
```  Column number              3      2      1      0
Weight (index)          16^3   16^2   16^1   16^0
Weight (actual value)   4096    256     16      1
```
Converting the hexadecimal value BEEF (Base 16) to decimal would be done like this:
```    B * 4096 = 11 * 4096
+ E *  256 = 14 *  256
+ E *   16 = 14 *   16
+ F *    1 = 15 *    1
=                48879
```
And converting the value 666 to hexadecimal would be done like this:
```  666 / 16 = 41 r 10    ^
41 / 16 =  2 r  9   /|\    Read digits in this direction, remembering to convert
2 / 16 =  0 r  2    |     to hex digits where required
(Stop here, as result is 0)
Hexadecimal value of 666 is 29A
```
The really good thing about hexadecimal is that it also allows you to convert to binary very easily. Each hexadecimal digit represents 4 bits, so to convert between hexadecimal and binary, you just need to convert each hex digit to 4 bits or every 4 bits to one hex digit (and remembering that 4 is an equal divisor of all the common lengths of binary numbers in a CPU). For example:
```  Hex number          5        9        D        F           4E
Binary value      0101     1001     1101     1111       01001110
```
When using hexadecimal values in PureBasic, you put a dollar sign in front of the number, for example \$BEEF.