Digital Logic 1/Numerical Systems

=Basic Ideas and Concepts=
 * The idea of a digit and what the digit can represent and its application to different numerical systems
 * How to determine an equivalent numerical representation in a different numerical system
 * How to add and subtract binary numbers
 * Understand the differences between signed and unsigned numbers
 * Understand how to represent signed numbers in binary
 * How to determine the one’s and two’s complement of a binary number

We will cover the four basic numerical systems most commonly used in computers (decimal, binary, octal, and hexadecimal) and determine the conversions between each of them. A numerical system is in a simple sense, another way to represent a value of something.

=Decimal= In childhood, people are often taught the fundamentals of counting by using their fingers. Counting from one to ten is one of many milestones a child achieves on their way to becoming educated members of society. We will review these basic facts on our way to gaining an understanding of alternate number systems.

The child is taught that the fingers and thumbs can be used to count from one to ten. Extending one finger represents a count of one; two fingers represents a count of two, and so on up to a maximum count of ten. No fingers (or thumbs) refers to a count of zero.

The child is later taught that there are certain symbols called digits that can be used to represent these counts. These digits are, of course:

$$0, 1, 2, 3, 4, 5, 6, 7, 8, 9\,\!$$

Ten, of course, is a special case, since it is comprised of two digits.

Before we go deeper, we need a few fundamental definitions.

Digit
A digit is a symbol given to an element of a number system.

Radix
The radix, or base of a counting system is defined as the number of unique digits in a given number system.

Back to our elementary example. We know that our hypothetical child can count from zero to ten using their fingers and thumbs. There are ten unique digits in this counting system, therefore the radix of our elementary counting system is ten.

We represent the radix of our counting system by putting the radix in subscript to the right of the digits. For example,

$$3_{10}\,\!$$

represents 3 in decimal (base 10).

Our special case (ten) illustrates a fundamental rule of our number system that was not readily apparent - what happens when the count exceeds the highest digit? Obviously, a new digit is added, to the left of our original digit which is "worth more", or has a higher weight than our original digit. (In reality, there *always* are digits to the left; we simply choose not to write those digits to the left of the first nonzero.)

$$10_{10} = 010_{10} = 0010_{10} = 0000000000000000000000000000000000000010_{10}\,\!$$

When our count exceeds the highest digit available, the next digit to the left is incremented and the original digit is reset to zero. For example:

$$9_{10} + 1_{10} = 10_{10}\,\!$$

Because we are dealing with a base-10 system, each digit to the left of another digit is weighted ten times higher. Using exponential notation, we can imagine the number 10 as representing:

$$10_{10} = 1 \times 10^1 + 0 \times 10^0 \,\!$$

$$314_{10} = 3 \times 10^2 + 1 \times 10^1 + 4 \times 10^0 \,\!$$

The fundamentals of decimal arithmetic will not be expanded on in this lecture.

=Binary= It is widely believed that the decimal system that we find so natural to use is a direct consequence of a human being's ten fingers and thumbs being used for counting purposes. One could easily imagine that a race of intelligent, six-fingered beings could quite possibly have developed a base-six counting system. From this perspective, consider the hypothesis: the most intuitive number system for an entity is that for which some natural means of counting exists.

Since our focus is electronic and computer systems, we must narrow our focus from the human hand to the switch, arguably the most fundamental structure that can be used to represent a count.

The switch can represent one of two states; either open, or closed. If we return to our original definition of a digit, how many digits are required to represent the possible states of our switch? Clearly, the answer is 2. We use the binary digits zero and one to represent the open and closed states of the switch.

Counting in Binary
Counting in binary like base 10 can be accomplished with your fingers with some differences. In decimal base 10 each finger represents a number in the first digit 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10. When counting in binary each finger represents a separate digit so counting is more like this.

WHOA, hold on, that pattern doesn't make sense? Well when you consider that each digit in binary is worth 2 times more than the previous (right to left always) then it makes sense. Lets compare number lines.

TO BE CONTINUED

Bits and bytes
A bit is a digit in the binary counting system.

A nybble (also spelled nibble) is a binary number consisting of four bits.

A byte, or octet is a binary number consisting of eight bits.

From our earlier definition of radix, the binary system has a radix of two. We use the radix in the subscript much like we do with decimals:

$$0110_2\,\!$$

Counting
Similar to decimals, binary digits are weighted. Each bit is weighted twice as much as the bit to the right of it:

$$0110_2 = 0 \times 2^3 + 1 \times 2^2 + 1 \times 2^1 + 0 \times 2^0\,\!$$

Counting from one to ten (base 10) in binary yields:

$$0001_2, 0010_2, 0011_2, 0100_2, 0101_2, 0110_2, 0111_2, 1000_2, 1001_2, 1010_2\,\!$$

Basic binary arithmetic
Binary addition, subtraction, multiplication and division operations work essentially the same as they do for decimals.

For addition, you add equally-weighted bits, much like decimal addition (where you add equally-weighted digits) and carry as required to the left.

$$0100_2 + 0111_2 \Rightarrow \begin{matrix} & & 0100 \\ & + & \underline{0111} \\ & = & 1011 \end{matrix} \,\!$$

As you can see, a carry is generated in the $$2^2\,\!$$ column which increments the $$2^3\,\!$$ column.

Subtraction works just like decimal arithmetic, using borrowing as required.

$$1011_2 - 0111_2 \Rightarrow \begin{matrix} & & 1011 \\ & - & \underline{0111} \\ & = & 0100 \end{matrix} \,\!$$

Here, a borrow is required, reducing the $$2^3\,\!$$ column to $$0-0=0\,\!$$ and changing the $$2^2\,\!$$ column to $$2-1=1\,\!$$.

Multiplication is straightforward also:

$$1011_2 \times 0111_2 \Rightarrow \begin{matrix} & & \; \; \; \; \; \; 1011 \\ & \times & \underline{\; \; \; \; \; \; 0111} \\ & & \; \; \; \; \; \; 1011 \\ & & \; \; \; \; 10110 \\ & & \underline{\; \; 101100 }\\ & = & 1001101 \\ \end{matrix} \,\!$$

Division is left as an exercise to the student [hint: use long division].

Conversion between binary and decimal
When you consider a binary number in exponential form, you can easily perform a decimal conversion:

$$0110_2 = 0 \times 2^3 + 1 \times 2^2 + 1 \times 2^1 + 0 \times 2^0\,\!$$

...simply add up the factors.

$$0 \times 2^3 + 1 \times 2^2 + 1 \times 2^1 + 0 \times 2^0 = 0 + 4 + 2 + 0 = 6\,\!$$

To convert from decimal to binary, you can repeatedly divide the decimal by two until the result of the division is zero. Starting from the rightmost bit, write 1 if the division has a remainder, zero if it does not. For example, to convert the decimal 74 into binary:

* 74 / 2 = 37 remainder 0; ->      0 * 37 / 2 = 18 remainder 1; ->     10 * 18 / 2 = 9 remainder 0; ->     010 * 9 / 2 = 4 remainder 1; ->    1010 * 4 / 2 = 1 remainder 0; ->   01010 * 2 / 2 = 1 remainder 0; ->  001010 * 1 / 2 = 0 remainder 1; -> 1001010

$$1001010_2 = 1 \times 2^6 + 1 \times 2^3 + 1 \times 2^1 = 64+8+2=74\,\!$$

=Octal=

The name octal implies eight, if you consider that an octagon has eight sides. Octal has a radix of eight, and uses the following octal digits:

$$0, 1, 2, 3, 4, 5, 6, 7\,\!$$

An octal number has the subscript 8.

Counting
Counting from one to ten (base 10) in octal yields:

$$01_8, 02_8, 03_8, 04_8, 05_8, 06_8, 07_8, 10_8, 11_8, 12_8\,\!$$

Octal arithmetic
Octal arithmetic, like binary arithmetic, follows the same rules and patterns of decimal arithmetic. As an exercise, verify the following:

$$32_8 + 56_8 = 110_8\,\!$$

$$32_8 \times 56_8 = 2254_8\,\!$$

$$52_8 / 25_8 = 2_8\,\!$$

Conversion between binary and octal
Each octal digit is representable by exactly three bits. This becomes obvious when you consider that the highest octal digit is seven, which can be represented in binary by $$111_2\,\!$$.

To convert a binary number to octal, group the bits in groups of three starting from the rightmost bit and convert each triplet to its octal equivalent.

$$100010011101_2 = (100 \ 010 \ 011 \ 101)_2 = (4 \ 2 \ 3 \ 5)_8 = 4235_8\,\!$$

To convert an octal number to binary, simply write the equivalent bits for each octal number.

$$752_8 = (111 \ 101 \ 010)_2 = 111101010_2\,\!$$

Conversion from decimal to octal
The repeated-division method described for binary will also work for octal, simply by changing the divisor to eight. To convert 67 (base 10 into octal):

* 67 / 8 = 8 remainder 3; ->       3 * 8 / 8 = 1 remainder 0; ->      03 * 1 / 8 = 0 remainder 1; ->     103

Can you prove that $$67_{10} = 103_8\,\!$$?

=Hexadecimal= The most commonly-used number system in computer systems is the hexadecimal, or more simply hex, system. It has a radix of 16, and uses the numbers zero through nine, as well as A through F as its digits:

$$0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F\,\!$$

Hex numbers can have the subscript 16, but more often have a leading *0x* to indicate their type.

$$0x0B45\,\!$$

Counting
Counting from zero to twenty (base 10) in hex yields:

$$0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, \,\!$$ $$0xB, 0xC, 0xD, 0xE, 0xF, 0x10, 0x11, 0x12, 0x13, 0x14\,\!$$

Hexadecimal arithmetic
Hex arithmetic, yet again, follows the same rules and patterns of decimal arithmetic. As an exercise, verify the following:

$$32_{16} + 56_{16} = 88_{16}\,\!$$

$$32_{16} \times 56_{16} = 10CC_{16}\,\!$$

$$4A_{16} / 25_{16} = 2_{16}\,\!$$

Conversion between hexadecimal and binary
Each hex digit is representable by exactly four bits. This becomes obvious when you consider that the highest hex digit represents fifteen, which can be represented in binary by $$1111_2\,\!$$.

To convert a binary number to hex, group the bits in groups of four starting from the rightmost bit and convert each group to its hex equivalent.

$$10001111_2 = (1000 \ 1111)_2 = (8 \ F)_{16} = 8F_{16}\,\!$$

To convert an hex number to binary, simply write the equivalent bits for each hex number.

$$7D_{16} = (0111 \ 1101)_2 = 01111101_2\,\!$$

=Signed and Unsigned Numbers= We can easily represent positive or unsigned numbers in any numerical system we wish, however, when a number is not positive, we introduce the concept of signed numbers. In the binary system, there are three ways to represent a signed number: sign and magnitude, one’s complement, and two’s complement.

Representations of a Signed Number
To represent a signed number using the sign and magnitude form in binary, we just assign the first bit of a binary number to represent whether that number is positive or negative. In sign and magnitude form, a 0 means a number is positive and a 1 means a number is negative.

For example, the number below in sign and magnitude form is:
 * $$ (1010)_2 = -2 \!$$

Because the first digit is 1, that means that the value of all the other digits excluding the first digit is negative.

To represent a signed number using the one’s complement form, we just flip all the bits of the binary number.

For example, if we want to represent -2 in one’s complement form we first find the binary equivalent of that number in binary, then we flip all the bits:
 * $$ 2= (0010)_2\!$$

Therefore:
 * $$-2 = (1101)_2\!$$

Two’s complement representation is a representation of a negative number in one’s complement form and adding 1 to the final one’s complement number.

So if we take the last example where:
 * $$ -2 = (1101)_2\!$$

We add one to that total which gives us:
 * $$(1110)_2\!$$

Refer to this page for further reading: Signed number representations