-binary system, numeration system based on powers of 2, in contrast to the familiar
decimal system,
which is based on powers of 10. In the binary system, only the digits 0
and 1 are used. Thus, the first ten numbers in binary notation,
corresponding to the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 in decimal
notation, are 0, 1, 10, 11, 100, 101, 110, 111, 1000, and 1001. Since
each position indicates a specific power of 2, just as the number 342
means (3 × 10
2) + (4 × 10
1) + (2 × 10
0),
the decimal equivalent of a binary number can be calculated by adding
together each digit multiplied by its power of 2; for example, the
binary number 1011010 corresponds to (1 × 2
6) + (0 × 2
5) + (1 × 2
4) + (1 × 2
3) + (0 × 2
2) + (1 × 2
1) + (0 × 2
0)
= 64 + 0 + 16 + 8 + 0 + 2 + 0 = 90 in the decimal system. Binary
numbers are sometimes written with a subscript "b" to distinguish them
from decimal numbers having the same digits. As with the decimal system,
fractions can be represented by digits to the right of the binary point
(analogous to the decimal point). A binary number is generally much
longer than the decimal equivalent; e.g., the number above, 1011010
b,
contains seven digits while its decimal counterpart, 90, contains only
two. This is a disadvantage for most ordinary applications but is offset
by the greater simplicity of the binary system in
computer
applications. Since only two digits are used, any binary digit, or bit,
can be transmitted and recorded electronically simply by the presence
or absence of an electrical pulse or current. The great speed of such
devices more than compensates for the fact that a given number may
contain a large number of digits.
2. Decimal Number System
-
Probably the biggest stumbling block most beginning
programmers encounter when attempting to learn assembly language is
the common use of the binary and hexadecimal numbering systems.
Understanding these numbering systems is important because their use
simplifies other complex topics including boolean algebra and logic
design, signed numeric representation, character codes, and packed
data.
This section discusses several important concepts
including the binary, decimal, and hexadecimal numbering systems,
binary data organization (into bits, nibbles, bytes, words, and double
words), signed and unsigned number systems, arithmetic, logical, shift,
and rotate operations on binary values, bit fields and packed
BCD (Binary Coded Decimal) data, and the ASCII (American Standard Code
for Information Interchange) character set. This is basic material and
the remainder of this tutorial depends upon your understanding of these
concepts. If you are already familiar with these terms from other courses
or study, you should at least skim this material before proceeding to the
next chapter. If you are unfamiliar with this material, or only vaguely
familiar with it, you should study it carefully before proceeding. All of
the material in this chapter is important! Do not skip over any
material.
Most modern computer systems do not represent
numeric values using the decimal system. Instead, they typically
use a binary or two's complement numbering system. To understand
the limitations of computer arithmetic, you must understand how
computers represent numbers.
Remember how mathematical operations are entered
into a computer:
- + is used for addition
- - is used for subtraction
- * is used for multiplication
- / is used for division
- ^ is used to raise to a power
There are four number bases commonly used in
programming. These are:
3. Octal Number System
-OCTAL NUMBER SYSTEM
The octal, or base 8, number system is a common system used with computers. Because of
its relationship with the binary system, it is useful in programming some types of
computers.
Look closely at the comparison of binary and octal number systems in table 1-3. You can
see that one octal digit is the equivalent value of three binary digits. The following
examples of the conversion of octal 2258 to binary and back again further
illustrate this comparison:
4. Hexadecimal Number System
-
The hexadecimal (base 16) number system operates the same way as the decimal
(base 10) number system, except it is based on sixteen instead of ten. The
operation of the decimal system is familiar.
The 4-digit base-10 number 5826 appears below, indicating how the value
of the number is derived from the values of its 4 digits.
Everybody knows that 5826 means five-thousand eight-hundred twenty-six. But
only because they have been taught that 1, 10, 100, and 1000 are part of the
calculation even though they are never written. All that is actually
written is a total of twenty-one (5 and 8 and 2 and 6). Only the interpretation
that people supply, which is purely mental and unwritten, informs what is
written with its intended value.
Hexadecimal operates the same way. Each digit is "weighted" by a
"multiplier," with the results all added together. The multipliers in
both systems are the powers of the system base (10 or 16). The powers of 10 are
1, 10, 100, 1000, etc. while those of 16 are 1, 16, 256, 4096, etc. So the same
digits "5826" used in base 16 represent a value calculated as follows:
Though the digits are the same (5826) the values come out quite different,
because the base and its "multiplier values" are different.
You will see hexadecimal numbers some of whose digits are letters instead of
numbers. That's because the number of digits needed by any number system is the
number's base. So base base 2 needs 2 digits, base 10 needs 10, and base
16 needs 16. Base 2 has 0 and 1. Base 10 has 0 through 9. Base 16 borrows 0
though 9 but needs another 6. For those, we could invent some symbols. However,
for convenience we employ the first 6 letters of the alphabet (A through F)
instead. When we run out of digits at 9, we use A as the next digit. So A
represents the value 10. B comes next, and represents 11. The hexadecimal digits
and thevalue they stand for are:
| Hexadecimal digit |
Value |
|
0 |
0 |
| 1 |
1 |
| 2 |
2 |
| 3 |
3 |
| 4 |
4 |
| 5 |
5 |
| 6 |
6 |
| 7 |
7 |
| 8 |
8 |
| 9 |
9 |
| A |
10 |
| B |
11 |
| C |
12 |
| D |
13 |
| E |
14 |
| F |
15 |
Here's the derivation of the value of another 4-digit hexadecimal number, but
this one uses some of the high-order digits A-F:
The highest you can count with a given number of digits (in any number
system) is the number in which every digit contains the maximum value in the
number system (1 in base 2, or 9 in base 10, or F in base 16). So the largest
number you can represent with 4 digits in base 16 is:
Counting any higher than that would require that you utilize more digits. The
very next number is 10000 in hexadecimal, or 65536 in decimal. It is a
well-known value in computer science and is called "64K."