Take a binary number:
This is made up of 4 bits. The left-hand bit is called the
most-significant bit or MSB and the right-hand bit is called the
least-significant bit or LSB.
The LSB, or bit 0, has the value (or weight) 1 or 20.
Each bit has an increasing weight and is a power of 2.
So bit 1, has a weight of 2 or 21,
bit 2, has a weight of 4 or 22 and
bit 3, has a weight of 8 or 23.
The number 1 0 0 1 equals 9 (decimal) as shown below.
Weight | 23 | 22 | 21 | 20 | |
Weight in decimal | 8 | 4 | 2 | 1 | |
Number | 1 | 0 | 0 | 1 | |
Add the weight of each '1' bit | 8 | + | 1 | = 9 |
In computing, 8 bits are used in a group and this is known as a byte. In the same way 4 bits as a group are called a nibble as this is half a byte!
You should also note that larger units than bytes are often used and are called words. A word is normally equal to the bus width of the processor (CPU) and could be 8, 16 or 32 bits wide.
decimal hexadecimal binary 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111Therefore a byte of data can be expressed as two hexadecimal digits.
Example: 10001010 Split into two 4-bit fields 1000 1010 = 8A (hexadecimal)
So, 2316 + 4516 = 6816.
Also, 2316 + 4916 = 6C16.
Notice that the hexadecimal digit "C" has to be used here to
represent the decimal number "12" as a carry is only generated
every 16 counts.
When a computer adds numbers together in bytes, then each carry has a value of 256 units.
0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | |
+ | ||||||||
0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | |
0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
Using different numbers,
1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | |
+ | ||||||||
1 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | |
1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
The carry will be added to the next most-significant column of bytes in the same way as we do a carry in decimal arithmetic.
The lesson here is that when two binary numbers are added together, the result potentially grows by one bit. So adding two 8-bit numbers together (bytes) gives a 9-bit result.
Normally the number to be subtracted is converted into its 2's complement form (negated) and an addition is performed.
For example (using 3-bits for simplicity):
1 | 0 | 1 | ||
x | 1 | 1 | 0 | |
0 | 0 | 0 | ||
1 | 0 | 1 | ||
1 | 0 | 1 | ||
1 | 1 | 1 | 1 | 0 |
In the multiplication process, multiplying two 8-bit numbers together will create a 16-bit result. More generally, multiplying an m-bit and an n-bit number together gives a result containing m+n bits.
If the subtraction is successful (i.e. the result does not go negative) then a '1' is entered into the corresponding bit of the quotient. If the trial subtraction is not successful, the a '0' is entered into the quotient. This process continues until all the bits of the dividend have been used leaving a remainder.
Since the most significant bit is set, if a 2's complement number is negative, this is an easy way of detecting the sign of a number.
The sign of a number is not an issue when adding or subtracting numbers. However in multiplication and division, the sign of the result will have to be determined from the signs of the two original numbers.
When two operands are used, these bit-wise functions operate in the same way as a 2-input logic gate.
A structure called an ALU (arithmetic and logical unit) is available which can perform most of the common arithmetic and logical functions.
An ALU comprises circuitry to perform arithmetic and logical operations, such a ADD, SUBTRACT, AND, OR, SHIFT. The construction and operation of an ALU may be understood by taking some examples of logical and arithmetic operations. A simple logic gate is shown below.
A and B are binary digits, and so is X - these are called bits. In a microprocessor, it is normal to deal with bytes (8 bits) rather than individual bits so the ALU equivalent to perform a byte-wide AND is
A0 to A7 comprise byte 1 and B0 to B7, byte 2. X0 to X7 is the result, again one byte. In this bitwise-AND operation, the circuitry for each bit is independent. These bits not always independent as we can see in the following example. Consider the circuit for an adder.
A and B are 1-bit inputs and SUM is the 1-bit output. CARRYin and CARRYout allow full adder stages to be cascaded as shown.
This is more commonly represented in the diagram below.
Suppose that an ALU supports the operations below. Some are unary operations on A only and others require two inputs, A and B:
Function | Code | Description |
ADD | 0 | A + B |
SUB | 1 | A - B |
SHL | 2 | Shift left A by 1 bit (= A x 2) |
SHR | 3 | Shift right A by 1 bit (= A / 2) |
AND | 4 | A AND B (bitwise) |
OR | 5 | A OR B (bitwise) |
XOR | 6 | A XOR B (bitwise) |
COM | 7 | 1's complement of A (bitwise) |
The ability to perform different operations on data is a function of a microprocessor. If the function codes were written in a list, to represent a sequence of operations, and these were presented to the ALU one at a time, then we would have the basis of a programmable microcomputer system.