Chapter 1
COMPUTER INTEGERS
LESSON 1.1: Binary Number System
TIME FRAME: 3 teaching hours CODE:
LEARNING OUTCOME(S): At the end of this lesson, the students are expected to:
1. define completely the notion of binary number system;
2. convert a decimal number into its binary equivalent, and vice versa;
3. perform arithmetic operations on binary numbers;
4. use the ASCII table to represent data into computer integers.
1.1.1 Introduction and Basic Terminologies
If intelligent beings other than us exist elsewhere in the universe, they may have
adopted a system for integers and other types of numbers like our own. They may have used
the same decimal integers (or base 10 numbers) that human beings are using now. However, it
will be certain as well that they understood the binary system of numbers – a numerical system
consisting of 0 and 1 only – since the notion of “yes or no”, “on and off”, and “true or false” are
clearly associated with intelligence.
Meanwhile, computers use the binary system extensively, as they are made from binary
digital components operating in two states: “on” and “off”. Furthermore, this system is used
primarily in representing, compressing, and transmitting data from one computer to another. In
this lesson, the basic notion of representing data into computer integers will be explored.
In order to gain a little understanding on how computers work, some basic
terminologies will be considered first.
Bit - basic unit of data or information in digital computers. It is either represented by a “0” or a
“1” in the binary system.
Byte - a group of eight bits which represents a character, or alphabet, in a data.
Word - a group of bytes that contains certain information with respect to the given data. The
length of a word corresponds to the number of bits used. For example, a word can have
length of 16 bits, 32 bits, and 64 bits.
1.1.2 Binary Number Systems
It is much well-known how the decimal system of numbers (or base 10 system) works.
Our customary system has digits chosen from 0 through 9 such that each digit determines a
factor of a power of 10; hence the name “base 10”. For example, the number 735 has three
digits 7, 3, and 5 which then indicates that the given number uses seven 102 ’s, three 101 ’s, and
five 100 ’s. This scheme can be illustrated in the following equality:
735 = 7 × 100 + 3 × 10 + 5 × 1 = 7 × 102 + 3 × 101 + 5 × 100 .
As stated in the previous section, the binary system only uses the digits 0 and 1. To write 735
into binary, use the same scheme as above, but this time will be under “base 2”, that is, replace
the 10’s with 2‘s and choose the factors of the powers of 2 from 0 and 1 only. Sure enough, a
number with only 0’s and 1’s in its digits is obtained. That number is precisely the binary
equivalent of 735.
Given an integer 𝑀 (preferably positive), a finite sequence 𝑏0 , 𝑏1 , …, 𝑏𝑛 of 0’s and 1’s is
said to provide the binary representation of 𝑀 if it is true that
𝑛
𝑀 = ∑ 2𝑘 𝑏𝑘 = 𝑏0 + 2𝑏1 + 4𝑏2 + ⋯ + 2𝑛 𝑏𝑛 .
𝑘=0
The corresponding binary number for 𝑀 is then conveniently written as
(𝒃𝒏 𝒃𝒏−𝟏 … 𝒃𝟐 𝒃𝟏 𝒃𝟎 )𝟐 or 𝒃𝒏 𝒃𝒏−𝟏 … 𝒃𝟐 𝒃𝟏 𝒃𝟎 B.
Thus, the number 735 when written in binary is equal to (1011011111)2 or
1011011111B since
735 = 512 + 128 + 64 + 16 + 8 + 4 + 2 + 1
= 1 × 29 + 0 × 28 + 1 × 27 + 1 × 26 + 0 × 25
+ 1 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 1 × 20
Table 1.1 shows some correspondence between binary and decimal notations
Decimal Binary Decimal Binary
0 0B 8 1000B
1 1B 9 1001B
2 10B 10 1010B
3 11B 11 1011B
4 100B 12 1100B
5 101B 13 1101B
6 110B 14 1110B
7 111B 15 1111B
Table 1.1 Binary notations of numbers 0 through 15
There is a more systematic way of finding the binary equivalent of a positive integer.
This can be done by successively divide the number by 2 and take note of the remainders. By
taking the same example as above, this method can be observed in the following table.
DIVISION BY 2 QUOTIENT REMAINDER PLACE OF BITS
735 ÷ 2 367 1 20
367 ÷ 2 183 1 21
183 ÷ 2 91 1 22
91 ÷ 2 45 1 23
45 ÷ 2 22 1 24
22 ÷ 2 11 0 25
11 ÷ 2 5 1 26
5÷2 2 1 27
2÷2 1 0 28
1÷2 0 1 29
Table 1.2 An alternative solution in finding the binary equivalent of the decimal 735
We need to stop when the quotient is now at zero. By reading backwards, we see that
the binary number equivalent to 735 is 1011011111B, which is similar to the result above.
Let us now turn our attention in obtaining the decimal equivalent of a given binary
number. Suppose the given binary is 110101B. Then by using the definition of binary
representation, we have
1 + 0 × 2 + 1 × 4 + 0 × 8 + 1 × 16 + 1 × 32 = 1 + 4 + 16 + 32 = 53.
Hence, the decimal equivalent of 110101B is 53. Apparently, these methods can also be used to
check the output of each other.
1.1.3 The Arithmetic of Binary Numbers
In this section, we shall deal with the operations on binary numbers as analogous to that
on decimal numbers.
Addition
When adding binary numbers, it is important to note that the digits of the sum of these
numbers must consist of 0’s and 1’s only. Thus, bit-wise sum yields the following facts:
0 + 0 = 0, 1 + 0 = 0 + 1 = 1, 1 + 1 = 10
As for the process of addition of binary numbers, the usual column-wise addition will be used.
When a certain column yields the third condition above, the carry-over principle will be applied.
For example, consider adding the binary numbers 1001B and 1110B. Then we have the
following solution:
1 1 0 0 1
+ 1 1 1 0
1 0 1 1 1
Hence, 1001B + 1110B = 10111B. To check whether the answer is correct, we shall
convert 1001B, 1110B, and 10111B into decimals. Now,
1001B = 1(8) + 0(4) + 0(2) + 1(1) = 9,
1110B = 1(8) + 1(4) + 1(2) + 0(1) = 14,
10111B = 1(16) + 0(8) + 1(4) + 1(2) + 1(1) = 23,
and so it is then clear that the correct answer is obtained.
Multiplication
Again, when multiplying binary numbers, the product must have digits of 0’s and 1’s
only. Now, arithmetic tells us that
0×0= 1×0= 0×1= 0 and 1 × 1 = 1.
As for the process, it is similar to the column-wise multiplication of decimal numbers.
For instance, we want to multiply 10101B and 10110B. Then we have the following
solution
1 0 1 0 1
× 1 0 1 1 0
1 0 0 0 0 0
1 1 0 1 0 1
1 1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
1 1 1 0 0 1 1 1 0
Hence, 10101B × 10110B = 111001110B. We should also expect that 111001110B is
462, the product of 21 and 22. That is,
10101B = 1(16) + 0(8) + 1(4) + 0(2) + 1(1) = 21,
10110B = 1(16) + 0(8) + 1(4) + 1(2) + 0(1) = 22,
111001110B = 1(256) + 1(128) + 1(64) + 0(32) + 0(16) + 1(8) + 1(4) + 1(2) + 1(1)
= 462.
Division
To divide binary numbers, we have to take a look at our divisor and count the length of
the bits. Then, use the long division similar to that in the decimal system.
As an example, suppose we want to divide 10101B by 11B. Arrange the divisor and the
dividend accordingly in the long division process, that is
1 1 1 0 1 0 1
Now, we cannot divide 1 by 11, so write “0” in the quotient. Similarly, we cannot divide
10 by 11, so put “0” in the quotient. Then we have 101 divided by 11, and we put “1” in the
quotient. Next, we multiply the first “1” in the quotient to 11 and subtract the result from 101
to obtain the difference of “10”. We record these observations below:
1
1 1 1 0 1 0 1
1 1
1 0
By following diligently the process of long division as previously shown, we have the
result below:
1 1 1
1 1 1 0 1 0 1
1 1
1 0 0
1 1
1 1
1 1
0
Therefore, 10101B ÷ 11B = 111B. To check, we have
10101B = 1(16) + 0(8) + 1(4) + 0(2) + 1(1) = 21,
11B = 1(2) + 1(1) = 3,
111B = 1(4) + 1(2) + 1(1) = 7,
which has clearly shown that we obtained the correct result.
As for the remainders, it is similarly written as in the decimal number system. For
instance, the quotient of 11111B when divided by 111B is 100B r. 11B (Verify!).
Ordinary Subtraction
In what follows, the rule applied for subtraction with positive difference.
Similar to addition, we shall consider the fact that difference of two (or more) binary
numbers must have digits of 0’s and 1’s only. For bit-wise subtraction, we shall consider the
following facts:
0 − 0 = 1 − 1 = 0, 1 − 0 = 1, 0 − 1 = 10 − 1 = 1.
As for the third condition, it pertains about the borrowing technique similar to the usual
subtraction in decimal numbers. That is, one must borrow 1 from the left digit of the minuend
in order to subtract 1 from 0.
To illustrate, subtract 10101B from 11110B. Then we have
1 1 1 1 0 1 1 1 0 10
− 1 0 1 0 1 ⟹ − 1 0 1 0 1
1 0 0 1
Thus, 11110B – 10101B = 1001B. We shall confirm if we obtain the correct result. Now,
11110B = 1(16) + 1(8) + 1(4) + 1(2) + 0(1) = 30
10101B = 1(16) + 0(8) + 1(4) + 0(2) + 1(1) = 21
1001B = 1(8) + 0(4) + (0)2 + 1(1) = 9
which clearly confirms our result.
But what about subtracting binary numbers that yield negative answers? That is, in
binary sense, what will be the binary equivalent to the expression 12 − 69? Or more
specifically, is there a negative binary number?
The answers for these questions is not as easy as one might think. However, we shall
delve into this discussion in order to complete the fundamental operations for binary numbers.
Negative Binary Numbers and Two’s Complement
Consider the decimal number 6 and its binary equivalent 110B. Our task here is to find a
way of writing −6 into its binary representation. This can be done using the principle of two’s
complement.
First off, it is a given fact now that every computer reads a whole number by either 32-
bit or 64-bit strings. This means that if 𝑥 is a decimal number, then it is customary to write a
binary representation by 𝑘 bits, where 𝑘 is a multiple of 4 (that is, 𝑘 = 4,8,16, …) and then we
can choose a particular value 𝑚 among the values of 𝑘 so that 2𝑚−1 is the least power of 2 such
that 𝑥 < 2𝑚−1. This particular method is known as the canonical form of the binary number
For example, the decimal number 6 must have a 4-bit binary representation (for this
case, 𝑚 = 4) since
6 < 8 = 23 = 24−1,
where 8 = 24−1 is the smallest such power satisfying the inequality. This means that, from the
representation 110B, we must have the 4-bit representation 0110B, where the added 0 to the
left means “0 × 8”.
As for the number 50, its canonical binary representation must be 00110010B since we
have
50 < 128 = 28−1 (here, 𝑚 = 8)
Now, we determine the “sign” of a binary number with the use of the least significant
bit (abbreviated as LSB). This is exactly* the left-most bit of the canonical form of the binary
number. If the LSB is 0, then the binary number is positive; otherwise negative if the LSB is 1. As
we can see from the canonical binary representations of 6 and 50, we see that both of them has
0 as the LSB, which is correct.
We shall now determine the negative binary representation of a decimal number using
the two’s complement. It has the following steps:
Step 1: Convert the decimal number into its canonical binary representation.
Step 2: “Flip” the bits in the canonical form into its opposite value. Thus, change the 0’s
into 1’s and 1’s into 0’s.
Step 3: Add 1 bit in the rightmost bit of the obtained binary representation in Step 2. The
result is exactly the negative binary representation of the negative decimal
number.
As an example, we look back on the canonical binary representation of 6 which is 0110B.
Now, we flip the bits into their opposite, which is now 1001B. Lastly, we add 1 to the rightmost
bit. The result will be 1010B. Take note that the LSB is 1, which means that the resulting binary
is indeed negative. From this point we obtain the binary representation of −6, which is 1010B.
One might question the answer for its validity, since common knowledge in binary
numbers tell us that 1010B is the binary representation of 10, which is clearly not −6. This is
where the role of “signs” in binaries comes in. Since the LSB of 1010B is 1, it means that the
power of 2 it associated with must be negative. For this case, the LSB 1 represents 23 , and so
we have the breakdown below:
1010B ⟹ −1 × 23 + 0 × 22 + 1 × 21 + 0 × 20 = −8 + 2 = −6
Hence, we obtain the desired result.
Subtraction Using Two’s Complement
This rule is specifically designed for subtracting binaries with negative result. Now,
remember in the arithmetic of integers that subtracting two integers is equivalent to adding the
minuend to the negative of the subtrahend. That is, given integers 𝑥 and 𝑦, we have
𝑥 − 𝑦 = 𝑥 + (−𝑦)
If both 𝑥 and 𝑦 are binary numbers, then −𝑦 is the negative binary equivalent of 𝑦. From this
point, we shall then employ the addition rule of binary numbers.
*
Depending on the use of the phrase “least significant”, an LSB is either the leftmost or the rightmost bit in a
binary number. However, when determining the sign of a binary number, it is preferred that the LSB is the leftmost
bit of the binary number.
For example, we want to find the difference between 1100B and 01000101B. But first,
add four 0’s to the left of the minuend 1100B so that it will line up to the subtrahend
01000101B. That is, we have
0 0 0 0 1 1 0 0
− 0 1 0 0 0 1 0 1
Next, we must find the negative of the subtrahend by using two’s complement.
Step 1: Change the subtrahend into canonical form
(This is done already, since the subtrahend is already in canonical form.)
Step 2: Flip the bits into its opposite bit.
Here, the result is: 10111010B
Step 3: Add 1 bit to the rightmost bit in the result of Step 2.
We have:
1 0 1 1 1 0 1 0
+ 1
1 0 1 1 1 0 1 1
Therefore, the negative of 01000101B is 10111011B.
Finally, we now add the minuend 00001100B to the negative of 01000101B. That is,
1 1 1
0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1
− 0 1 0 0 0 1 0 1 ⟹ + 1 0 1 1 1 0 1 0
1 1 0 0 0 1 1 1
Hence, we have 1100B − 01000101B = 11000111B.
Let us check whether we obtain the correct answer. We know that 1100B = 12 and
01000101B = 69. Now, 12 − 69 = −57 and so we must verify that 11000111B = −57.
As we have discussed earlier, the LSB of a binary number dictates its sign whether
positive or negative. Since 1 is the LSB of 11000111B, then it must be negative. Since we have
an 8-bit binary number, it follows that the LSB corresponds to 27 = 128 and is negative. Thus,
11000111B ⟺ −1 × 27 + 1 × 26 + 0 × 25 + 0 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20
⟺ −128 + 64 + 4 + 2 + 1 = −57
and so we have a correct answer.
Meanwhile, consider a smaller subtraction: 1110B – 0110B. First, we shall convert the
minuend 1110B into its canonical form (why?), which is 00001110B. Next, we add four 0’s on
the left of the subtrahend 0110B to line up with the minuend; thus, we have the subtrahend
00000110B. Then, we find the negative of 00000110B, which is 11111010B (verify!). Hence, we
have the following subtraction:
1 1 1 1 1 1
0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0
− 0 0 0 0 1 0 0 0 ⟹ + 1 1 1 1 1 0 1 0
1 0 0 0 0 1 0 0 0
Notice that we have an excess 1 bit on the leftmost part of the sum. Since we perform the
subtraction operation canonically, then we must disregard the excess bit, leaving us the binary
number 00001000B, or precisely 1000B, as the desired difference.
To check our result, we know that 1110B, 0110B, and 1000B are the binary equivalents
of 14, 6, and 8, respectively. Hence, we have 14 – 6 = 8, which means we obtain the correct
answer.
From our example above, we see that subtraction using two’s complement is MORE
efficient than ordinary subtraction. In reality, this method is more appropriate since one does
not bother anymore if the minuend is smaller than the subtrahend, especially if we deal with
large binaries.
1.4 The ASCII Table
In a computer system, every character in the human literature must be assigned with a
specific number or integer. The text files that a human person reads and write are actually
stored, loaded into memory and manipulated as a sequence of numbers. When the computer
displays the data on screen as text, it changes the numbers into characters so that humans can
understand it.
In order for text files to be reliably stored and processed by computers, it is important
that they all interpret the data in the same way. To achieve this, a standardized mapping was
created that defined what numbers should be used to represent all the characters in the English
language. This mapping was called the American Standard for Information Interchange, or ASCII
for short.
Figure 1.1 Sample ASCII code chart used by the General Electric (GE) electronics company, circa 1972
https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png
The ASCII table is split into two sections, the lower ASCII table and the upper ASCII table.
The lower ASCII table defines all numbers between 0 and 127 inclusive. This is the officially
standardized section of the ASCII table and represents all the most common characters.
Meanwhile, the upper ASCII table has not been officially standardized, and tends to vary from
computer to computer and from font to font. This will not be shown here; however, one may
quickly obtain a comprehensive ASCII table on the Internet.
ASCII # Character ASCII # Character
0 Null (NUL) 32 (Space)
1 Start of Heading (SOH) 33 !
2 Start of Text (STX) 34 “
3 End of Text (ETX) 35 #
4 End of Transmit (EOT) 36 $
5 Enquiry (ENQ) 37 %
6 Acknowledge (ACK) 38 &
7 Audible Bell (BEL) 39 ‘
8 Backspace (BS) 40 (
9 Horizontal Tab (HT) 41 )
10 Line Feed (LF) 42 *
11 Vertical Tab (VT) 43 +
12 Form Feed (FF) 44 ,
13 Carriage Return (CR) 45 -
14 Shift In (SI) 46 .
15 Shift Out (SO) 47 /
16 Data Link Escape (DLE) 48 0
17 Device Control 1 (DC1) 49 1
18 Device Control 2 (DC2) 50 2
19 Device Control 3 (DC3) 51 3
20 Device Control 4 (DC4) 52 4
21 Neg. Acknowledge (NAK) 53 5
22 Synchronous Idle (SYN) 54 6
23 End Trans. Block (ETB) 55 7
24 Cancel (CAN) 56 8
25 End of Medium (EM) 57 9
26 Substitution (SUB) 58 :
27 Escape (ESC) 59 ;
28 File Separator (FS) 60 <
29 Group Separator (GS) 61 =
30 Record Separator (RS) 62 >
31 Unit Separator (US) 63 ?
Table 1.3 The first 64 entries of the lower ASCII Table
ASCII # Character ASCII # Character
64 @ 96 `
65 A 97 a
66 B 98 b
67 C 99 c
68 D 100 d
69 E 101 e
70 F 102 f
71 G 103 g
72 H 104 h
73 I 105 i
74 J 106 j
75 K 107 k
76 L 108 l
77 M 109 m
78 N 110 n
79 O 111 o
80 P 112 p
81 Q 113 q
82 R 114 r
83 S 115 s
84 T 116 t
85 U 117 u
86 V 118 v
87 W 119 w
88 X 120 x
89 Y 121 y
90 Z 122 z
91 [ 123 {
92 \ 124 |
93 ] 125 }
94 ^ 126 ~
95 _ 127 Forward Delete (DEL)
Table 1.4 The remaining 64 entries of the lower ASCII Table
The numbers from 0 to 31 inclusive represent non-printing characters, meaning
characters that are not directly displayed. These characters control how the data should be
interpreted. All the rest of the characters above 31 in the lower ASCII table are printable, except
for forward delete (ASCII #127), an apparently late addition, which means to delete the next
character.
The ASCII table can be used to obtain the byte equivalent of a character. For example,
the character “a” has a numeric equivalent of 97 in the lower ASCII table. The binary number
associated to 97 is computed below.
DIVISION BY 2 QUOTIENT REMAINDER PLACE OF BITS
97 ÷ 2 48 1 20
48 ÷ 2 24 0 21
24 ÷ 2 12 0 22
12 ÷ 2 6 0 23
6÷2 3 0 24
3÷2 1 1 25
1÷2 0 1 26
Hence, the byte equivalent of “a” is 01100001B. Notice that, instead of having
1100001B as the desired equivalent, the binary equivalent of “a” is written in 8-bit canonical
form. This is because all characters and functions in the ASCII table is written in terms of bytes,
which is the smallest measure of memory in terms of computer storage.