This blog will attempt to explain how to utilize unsigned bytes in four basic algorithmic operations. This walk through works for single bytes (eight bits each) to represent integers that do not sum up to, or who's products are more than or equal to, 255, whose difference is not negative, and that divide evenly. This is merely for the sake of simplicity.
To get things started, you need to know how a byte, or any amount of bits, represents an integer. First, look at the space in which the bit is in (reading right to left). The first bit is always position 0; every space after is one more. To find the value that a bit represents, use 2^n, where n is the space of the bit.
In more solid terms, we treat bits like indices. In an index, a value represents a space, meaning that we begin at 0 even though that's one space.
So the 1 in the byte 00001000 represents 8 because it's in position 3 and 2^3=8.
Additionally, to find the integer a byte represents, simply understand that 1 MEANS TRUE; THE NUMBER IT REPRESENTS IS PART OF THE SUM. 0 MEANS FALSE; THE NUMBER IT REPRESENTS IS NOT PART OF THE SUM.
What integers do these bytes represent?
00011001
00000101 25 and 5, respectively. These are the bytes we're going to use for every example.
Addition To add binary values, align the bytes in a stack.
Then look at each bit's position.
For reference...
0+0=0 0+1=1 1+0=1 1+1=0 ~ Carry a 1 to the next bit.
To add 25 and 5, arrange like so...
00011001
00000101 In the 0 position, add the 1's. That same position in your sum should have a 0, while you carry a 1 to the next position.
Then you add those values, and move on like so.
00011110 ~ 30
Subtraction
In order to subtract 5 from 25, first we align the bits.
00011001
00000101
1-1=0 0-0=0 1-0=1 0-1=1 ~ Take a 1 from the next significant bit.
In position 2 of both bits, we're subtracting 1 from 0. We put one down in our remainder, but in position 3 of the 25 byte, we change the 1 to 0.
Eventually we get:
00010100 Or 20.
Multiplication
To multiply bytes, we align the bytes.
00011001
00000101
Do as you multiply ten by ten: by multiplying each number on top by each number on the bottom right to left.
So, multiply each bit in the 25 byte by the bit in the 0 position:
00011001
Like in regular math, go the the space bellow and shift to the left one position in your product, then multiply by the bit in position 1:
..00011001
00000000
And for each bit:
....00011001 ..00000000 00011001
Stop at the last bit that contains 1 to save the trouble, and add:
01111101 Or 125
Division
In order to divide 25 by 5, station as much of the byte as you need in front of the 25 byte.
101 | 00011001
And look for an arrangement of bits that is more than or equal to five.
101 | 00011001 ...............^^^
110 equals 6.
Put a one in the space above the rightmost bit, and then subtract