Bits and bytes

If you already know how to calculate in hexadecimal, you're probably going to fall asleep while reading this page. To prevent that, skip this page if you already know how to.

Calculating
You probably already know that you can split up the number 3333 in four digits. The rightermost digit is worth 3 units of quantity, which quantity doesn't matter for now. The 3 left of that one, is worth 3 * 10 units of quantity, one place left is worth 3 * 100 units of quantity, and the most left 3 is worth 3 * 1000 units of quantity. Let me spell this out for you: 3333 = 3000    + 300      + 30       + 3      = 3 * 1000 + 3 * 100  + 3 * 10   + 3 * 1      = 3 * 10^3 + 3 * 10^2 + 3 * 10^1 + 3 * 10^0 You see that every digit is worth it's quantity times 10 to the power of it's place, counted from right. Because it is 10 to the power of etc., this way of displaying numbers is called displaying with Base-10.

So what if I displayed the number 3333 in Base-8? Let's find out: 3333 = 3 * 8^3 + 3 * 8^2 + 3 * 8^1 + 3 * 8^0     = 3 * 512 + 3 * 64  + 3 * 8   + 3 * 1      = 1536    + 192     + 24      + 3      = 1755 Do note that the number 1286 is invalid if you place it in base-8, simply because there is no 8. If you counted from 1 to 10 in base-8, you would skip the 8 and the 9: 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21... etc.

Another thing we will need to do is learning how to get the base-10 number 1648 in base-8. In order to do this, we first try to figure out how many digits the number will eventually have. Then, we must figure out the last, (left) digit, and work from left to right. The number of the digit can be found by using: | /^ |, in which the remaining numer is:  - *^. But I know that this sounds like abacadabra for you, so let me show you: Number of digits: 4 (because 8^4 = 4096 > 1648) Last (leftermost) digit: |1648 / 8^3| = |1648 / 512| = |3.21875| = 3 Remaining number: 1648 - 3 * 8^3 = 112 One to last digit:      |112 / 8^2| = |112 / 64| = |1.75| = 1 Remaining number: 1648 - (3 * 8^3 + 1 * 8^2) = 48 2nd to last digit:      |48 / 8^1| = |48 / 8| = |6| = 6 Remaining number: 1648 - (3 * 8^3 + 1 * 8^2 + 6 * 8^1) = 0 Rightermost digit:      |0 / 8^0| = |0 / 1| = |0| = 0 So: 3160. You still don't understand it, right? Let me do it step by step. Number of digits: 4 (because 8^4 = 4096 > 1648) By now, I found out how many digits the eventual number will have. The digits representing the quantity * 8^4 will be 0, because 8^4 is bigger 1648. Suppose you've got 7983 in base-10, then the quantity representing 10^4 will be 0, because 10.000 > 7983 (07983). Formula:                | /^ | Last (leftermost) digit: |1648 / 8^3| + |1648 / 512| = |3.21875| = 3 So we're going to calculate the leftermost digit. This one is calculated by deviding the whole number (because it's the first digit we calculate) by the base-number (8) to the power of it's relative place to the rightermost digit (3). Sup

pose you've got the number 7983, the relative distance from the 7 to the 3, is 3 digits. The distance between the 9 and the 3 is 2 digits, between the 8 and the 3 1, and between the 3 and the 3 0 digits. So, when we fill that in in the formula, we find out that the leftermost digit is 3. Formula:                 - *^ Remaining number:       1648 - 3 * 8^3 = 112 Formula:                | /^ | One to last digit:      |112 / 8^2| = |112 / 64| = |1.75| = 1 Now for the one to last digit. We first need to calculate how many quantitys we still have left to substract from. We do that by the formula I gave first. The previous number is the previous remaining number we calculated, but since we did not calculate any, we take the origional number (1648). The previous calculation is next, which just means the previous calculated digit (3). The base-number we know (8), and the digitplace is the place of the digit relative to the rightermost digit (3). So, filling in that, we get to see that we have 112 quantities left to spread over the remaining 3 digits. I hope this made any sence.:)

Anyway, after that, we can now calculate how many quantities should be displayed at the first to last digit (1). Remaining number: 1648 - (3 * 8^3 + 1 * 8^2) = 48 2nd to last digit:      |48 / 8^1| = |48 / 8| = |6| = 6 We just used the same method again, so we calculated that after giving 1 * 8^2 away, there's 48 left over to spread. Remaining number: 1648 - (3 * 8^3 + 1 * 8^2 + 6 * 8^1) = 0 Rightermost digit:      |0 / 8^0| = |0 / 1| = |0| = 0 And, for the last time, we used those two formulas to calculate the last digit. So: 3160 After we add all the digits behind each other, and we see that the final number is 3160.

You're probably wondering why we're learning all this. This is because the calculator loves to calculate in Base-2 and Base-16. So, let's take a number, 89, and put it in binary (base-2).

Number:          89 Number of digits: 7 (because 2^7 = 128 > 89) Digit:           |89 / 2^6| = |89 / 64| = |1.390625| = 1 Remaining number: 89 - 1*2^6 = 89 - 1*64 = 89 - 64 = 25 Digit:           |25 / 2^5| = |25 / 32| = |0.78125| = 0 Remaining number: 25 - 0*2^5 = 25 - 0*32 = 25 - 0 = 25 Digit:           |25 / 2^4| = |25 / 16| = |1.5625| = 1 Remaining number: 25 - 1*2^4 = 25 - 1*16 = 25 - 16 = 9 Digit:           |9 / 2^3| = |9 / 8| = |1.125| = 1 Remaining number: 9 - 1*2^3 = 9 - 1*8 = 9 - 8 = 1 Digit:           |1 / 2^2| = |1 / 4| = |0.25| = 0 Remaining number: 1 - 0*2^2 = 1 - 0*4 = 1 - 0 = 1 Digit:           |1 / 2^1| = |1 / 2| = |0.5| = 0 Remaining number: 1 - 0*2^1 = 1 - 0*2 = 1 - 0 = 1 Digit:           |1 / 2^0| = |1 / 1| = |1| = ''1 ''So, the eventual number is 1011001 (base-2) Now let's take the same number, 89, and put it in hexadecimal (base-16). A good student would now say: "How do we indicate those numbers between 9 and 10 then?" If we counted from 0 to 20 in hexadecimal, this would be the result: 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20. Number: 89 Number of digits: 2 (because 16^2 = 256 > 89) Digit:           |89 / 16^1| = |89 / 16| = |5.5625| = 5 Remaining number: 89 - 5*16^1 = 89 - 5*16 = 89 - 80 = 9 Digit:           |9 / 16^0| = |9 / 1| = |9| = 9 So, the eventual number is 59. Next question: "How do you know in which base a number is displayed?" We do that by adding a symbol either before or after a number. So, 89 can be displayed as $59, 59h, %1011001 or 1011001b. You MUST choose either a prefix or a suffix, and you CANNOT mix them (like $59h). I personally prefer the prefixes, so I'll be using them everywhere.

Note that you can also use base-8 numbers, but since the calculator doesn't use them, I discourage it.

Bits, nibbels, bytes and words
Memory is just a continuous flood of 0's and 1's, as I decide to look at it in base-2 format, for now. In this endless flow, every single place to store a 0 or a 1, is called a bit (binarydigit). You can pair up a certain number of bits, to make programming as a whole easier. These accumulations have all been given a name. When you load certain numbers into 8-bit places, you most of the time really fill up the 8 bits by dislpaying them all when you're writing in base-2 format. The 89 example could be writed as %1011001, but I personally prefer to write them as %01011001, just so that I know I filled up the 8-bits.

The same works for base-16 numbers. If you're loading a hexadecimal number into an 8-bit place, you normally write $0F in stead of $F.

I prefer to do the same thing when loading into a 16-bit place. Although you usually don't load base-2 numbers in 16-bit places, you can: %0111111111111111 (which equals 32768). And, $0403 in stead of $403.

One last note: if you have an accumulation of bits, let's say a byte, then the first digit is the Most Significant Bit (MSB) and the last bit is the Lease Significant Bit (LSB). You can also call the first nibble in a byte the most significant, or the first byte in a word, etc.

Negative numbers
There is a, quite tricky, way to display negative numbers. That's where signed numbers come in.

A number can either be unsigned or signed. When the number is unsigned, the numbers value lies between 0 and 255. When a number is signed, it's value lies between -128 and 127.

When a number is signed, its Most Significant Bit becomes the Sign Bit, and the bit next to that the Most Significant Bit. If the sign bit is set (i.e. it equals 1), we are dealing with a negative number. Alternatively, when it's reset (i.e. it equals 0), the number is positive. A few examples: You see that %10000000 equals -128 and from there on, you count. So, -127 equals %10000001, -126 equals %10000010, etc.

By now, you're probably wondering how the calculator knows whether a number is signed or unsigned. The answer is very likely to be more simple than you could ever imagine: "It doesn't. You do." It's like this with every aspect of programming. The calculator doesn't know anything about what you want this section of data and that piece of coding to do. The only one who does is you. It's this famous sentence from the "ASM in 28 days" tutorial that I found really to the point... "Data is whatever you define it to be."

The miracle of signed and unsigned numbers
The greatest miracle of these signed and unsigned numbers is that you can just mix them in whatever operation you want. Let's take a look at adding. Binary:	Unsigned:	Signed: %10101001	169		-87 %01001100+ 	 76+ 		 76+ %11110101	245		-11 The more careful coders will have noticed that the sign bit will be corrupted when the answer will be outside the range of -128 to 127. But, there is another miracle that deals with this; the flags.

As you decide if the number is either signed or unsigned, you can use signed numbers everywhere you want (even in 16-bit instructions). You must pay attention to how the calculator deals with them though. More information about this later on as well.