0% found this document useful (0 votes)
10 views

CSC 204 Session 2

Uploaded by

jfixcoding
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CSC 204 Session 2

Uploaded by

jfixcoding
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CSC 204: Fundamentals of Data Structures

Study Session 2: Data Representation in Memory

Introduction
How data is stored varies from one environment to another, with each environment having its own
set of rules and standards. Data representation is generally how information is conceived,
manipulated, and recorded. The term can also be defined as the form in which data and information
is kept in a certain environment.

In this study session you will learn about data representation and its usefulness to Computer
Science. This Study Session will discuss several important concepts including the binary and
hexadecimal numbering systems, binary data organization (bits, nibbles, bytes, words, and double-
words), and logical operations.

Learning Outcomes for Study Session 2


On completion of this study session, you should be able to:
2.1 Describe Data Representation
2.2 Discuss the Numbering System
2.3 List and Explain Data Organisation
2.4 Describe the basic Logical Operations on Bits

Page 39 of 269
CSC 204: Fundamentals of Data Structures

2.1 Introduction to Data Representation


Data Representation refers to the methods used internally to represent information stored in a
computer. Computers store lots of different types of information such as numbers, text, graphics of
many varieties (pictures, videos, and animation) and sound. At least, these all seem different to us.
However, ALL types of information stored in a computer are stored internally in the same simple
format: a sequence of 0's and 1's.

How can a sequence of 0's and 1's represents things as diverse as your photograph, your
favourite song, a recent movie, or your term paper?

It all depends on how you interpret the information. Computers use numeric codes to represent all
the information they store. These codes are similar to those you may have used as a child to encrypt
secret notes: let 1 stand for A, 2 stand for B, etc. With this code, any written message can be
represented numerically.

The codes used by computers are a bit more sophisticated, and they are based on the binary number
system (base two) instead of the more familiar (for the moment) decimal system. Computers use a
variety of different codes. Some are used for numbers, others for text, and still others for sound and
graphics.

Box 2.1: Data Representation

Data Representation refers to some means of storing data in a form which a program on a computer
can find it again

Data representation can also be described as the way the physical properties of a medium are used
to represent data. It is the manner in which data is expressed symbolically by binary digits in a
computer.

Probably the biggest stumbling block most beginners encounter when attempting to learn assembly
language is the common use of the binary and hexadecimal numbering systems. Many

Page 40 of 269
CSC 204: Fundamentals of Data Structures

programmers think that hexadecimal (or hex) numbers represent absolute proof that God never
intended anyone to work in assembly language.

While it is true that hexadecimal numbers are a little bit different from what you may be used to,
their advantages outweigh their disadvantages by a large margin. Nevertheless, 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.

Pilot Question 2.1


i. In what format does computer Store information?
ii. The way the physical properties of a medium are used to represent data is referred to as
_______________________.

2.2 Numbering Systems


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.

2.2.1 A Review of the Decimal System


You’ve been using the decimal (base 10) numbering system for so long that you probably take it for
granted. When you see a number like “123”, you don’t think about the value 123; rather, you
generate a mental image of how many items this value represents.

In reality, however, the number 123 represents:

1*102 + 2*101 + 3*100


OR
100+20+3

Each digit appearing to the left of the decimal point represents a value between zero and nine times
an increasing power of ten. Digits appearing to the right of the decimal point represent a value
between zero and nine times an increasing negative power of ten.

For example, the value 123.456 means:

Page 41 of 269
CSC 204: Fundamentals of Data Structures

1*102 + 2*101 + 3*100 + 4*10-1 + 5*10-2 + 6*10-3


OR
100 + 20 + 3 + 0.4 + 0.05 + 0.006

2.2.2 The Binary Numbering System


The binary numbering system works just like the decimal numbering system, with two exceptions:
binary only allows the digits 0 and 1 (rather than 0-9), and binary uses powers of two rather than
powers of ten. Therefore, it is very easy to convert a binary number to decimal. For each “1” in the
binary string, add in 2n where “n” is the zero-based position of the binary digit. For example, the
binary value 110010102 represents:

1*27 + 1*26 + 0*25 + 0*24 + 1*23 + 0*22 + 1*21 + 0*20

= 128 + 64 + 8 + 2

=20210

To convert decimal to binary is slightly more difficult. You must find those powers of two which,
when added together, produce the decimal result. The easiest method is to work from the large
power of two down to 20.

Consider the decimal value 1359:


1. 210=1024, 211=2048. So 1024 is the largest power of two less than 1359. Subtract 1024 from
1359 and begin the binary value on the left with a “1”digit. Binary =”1”, Decimal result is
1359 - 1024 = 335.
2. The next lower power of two (29= 512) is greater than the result from above, so add a “0” to
the end of the binary string. Binary = “10”, Decimal result is still 335.
3. The next lower power of two is 256 (28). Subtract this from 335 and add a “1” digit to the
end of the binary number. Binary = “101”, Decimal result is 79.
4. 128 (27) is greater than 79, so tack a “0” to the end of the binary string. Binary = “1010”,
Decimal result remains 79.
5. The next lower power of two (26 = 64) is less than79, so subtract 64 and append a “1” to the
end of the binary string. Binary = “10101”, Decimal result is 15.
6. 15 is less than the next power of two (25 = 32) so simply add a “0” to the end of the binary
string. Binary = “101010”, Decimal result is still 15.

Page 42 of 269
CSC 204: Fundamentals of Data Structures

7. 16 (24) is greater than the remainder so far, so append a “0” to the end of the binary string.
Binary = “1010100”, Decimal result is 15.
8. 23 (eight) is less than 15, so stick another “1” digit on the end of the binary string. Binary =
“10101001”, Decimal result is 7.
9. 22 is less than seven, so subtract four from seven and append another one to the binary
string. Binary = “101010011”, decimal result is 3.
10. 21 is less than three, so append a one to the end of the binary string and subtract two from
the decimal value. Binary = “1010100111”, Decimal result is now 1.
11. Finally, the decimal result is one, which is 20, so add a final “1” to the end of the binary
string. The final binary result is “10101001111”

2.2.3 The Hexadecimal Numbering System


To represent the value 20210 in binary, it requires eight binary digits (110010102). The decimal
version requires only three decimal digits and, thus, represents numbers much more compactly than
does the binary numbering system.

This fact was not lost on the engineers who designed binary computer systems. When dealing with
large values, binary numbers quickly become too unwieldy. Unfortunately, the computer thinks in
binary, so most of the time it is convenient to use the binary numbering system. Although we can
convert between decimal and binary, the conversion is not a trivial task. The hexadecimal (base 16)
numbering system solves these problems.

Hexadecimal numbers offer the two features we’re looking for: they’re very compact, and it’s
simple to convert them to binary and vice versa. Because of this, most binary computer systems
today use the hexadecimal numbering system. Since the radix (base) of a hexadecimal number is
16, each hexadecimal digit to the left of the hexadecimal point represents some value times a
successive power of 16. For example, the number 123416 is equal to:

1 * 163 + 2 * 162 + 3 * 161 + 4 * 160

OR

4096 + 512 + 48 + 4 = 466010

Each hexadecimal digit can represent one of sixteen values between 0 and 1510. Since there are
only ten decimal digits, we need to invent six additional digits to represent the values in the range

Page 43 of 269
CSC 204: Fundamentals of Data Structures

1010 through 1510. Rather than create new symbols for these digits, we’ll use the letters A through
F. The following are all examples of valid hexadecimal numbers:

123416 DEAD16 BEEF16 0AFB16 FEED16 DEAF16

Since you will often need to enter hexadecimal numbers into the computer system, you’ll need a
different mechanism for representing hexadecimal numbers. After all, on most computer systems
you cannot enter a subscript to denote the radix of the associated value.

Adopt the following conventions:


i. All numeric values (regardless of their radix) begin with a decimal digit.
ii. All hexadecimal values end with the letter “h”, e.g., 123A4h3.
iii. All binary values end with the letter “b”.
iv. Decimal numbers may have a “t” or “d” suffix.

Examples of valid hexadecimal numbers:

1234h 0DEADh 0BEEFh 0AFBh 0FEEDh 0DEAFh

As you can see, hexadecimal numbers are compact and easy to read. In addition, you can easily
convert between hexadecimal and binary. Consider the following table:

Table 2.1: Binary/Hexadecimal Conversion


BINARY HEXADECIMAL

0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 7

Page 44 of 269
CSC 204: Fundamentals of Data Structures

1000 8

1001 9

1010 A

1011 B

1100 C

1101 D

1110 E

1111 F

Pilot Question 2.2


i. Convert these binary numbers to their equivalent decimal values.
a. 11011
b. 10101010

2.3 Data Organization


In pure mathematics, a value may take an arbitrary number of bits. Computers, on the other hand,
generally work with some specific number of bits. Common collections are single bits, groups of
four bits (called nibbles), groups of eight bits (called bytes), groups of 16 bits (called words), and
more. The sizes are not arbitrary. There is a good reason for these particular values.

2.3.1 Bits
The smallest “unit” of data on a binary computer is a single bit. Since a single bit is capable of
representing only two different values (typically zero or one), you may get the impression that there
are a very small number of items you can represent with a single bit. This is not true! There are an
infinite number of items you can represent with a single bit. With a single bit, you can represent any
two distinct items. Examples include zero or one, true or false, on or off, male or female, and right
or wrong.

Page 45 of 269
CSC 204: Fundamentals of Data Structures

Figure 2.1: Bits of 0s and 1s

Although computers usually provide instructions that can test and manipulate bits, they generally
are designed to store data and execute instructions in bit multiples

Box 2.2: Bits

A bit is the basic unit of information in computing and digital communications. A bit can have only
one of two values, and may therefore be physically implemented with a two-state device. The most
common representation of these values are 0and1. The term bit is also known as binary digit.

2.3.2 Nibbles
A nibble is a collection of four bits. It wouldn’t be a particularly interesting data structure except
for two items: BCD (binary coded decimal) numbers and hexadecimal numbers. It takes four bits to
represent a single BCD or hexadecimal digit. With a nibble, we can represent up to 16 distinct
values. In the case of hexadecimal numbers, the values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and
F are represented with four bits. BCD uses ten different digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) and
requires four bits. In fact, any sixteen distinct values can be represented with a nibble, but
hexadecimal and BCD digits are the primary items we can represent with a single nibble.

Page 46 of 269
CSC 204: Fundamentals of Data Structures

Box 2.3 Nibble

In computing, a nibble is a four-bit aggregation or half an octet. As a nibble contains 4 bits, there
are sixteen (24) possible values, so a nibble corresponds to a single hexadecimal digit (thus, it is
often referred to as a "hex digit" or "hexit").

2.3.3 Bytes
The byte is a unit of digital information in computing and telecommunications that most commonly
consists of eight bits. Historically, the byte was the number of bits used to encode a single character
of text in a computer and for this reason it is the smallest addressable unit of memory in many
computer architectures.

Probably the most important use for a byte is holding a character code. Characters typed at the
keyboard; displayed on the screen, and printed on the printer all have numeric values. The size of
the byte has historically been hardware dependent and no definitive standards existed that mandated
the size.

This means that the smallest item that can be individually accessed is an eight-bit value. To access
anything smaller requires that you read the byte containing the data and mask out the unwanted
bits. The bits in a byte are normally numbered from zero to seven

Figure 2.2: Relationship between bits, bytes and word

Page 47 of 269
CSC 204: Fundamentals of Data Structures

2.3.4 Words
A word is a group of 16 bits. The number bits in a word starting from zero on up to fifteen. The bit
numbering appears in Figure 2.2

Figure 2.3: Bit Numbers in a Word

Like the byte, bit 0 is the low order bit and bit 15 is the high order bit. When referencing the other
bits in a word, use their bit position number.

Naturally, a word may be further broken down into four nibbles as shown in Figure 2.3

Figure 2.4: Nibbles in a Word

Nibble zero is the low order nibble in the word and nibble three is the high order nibble of the
word. The other two nibbles are “nibble one” or “nibble two”.

With 16 bits, you can represent 216 (65,536) different values. This could be the values in the range
0...65,535 (or, as is usually the case, -32,768...+32,767) or any other data type with no more than
65,536 values. The three major uses for words are integer values, offsets, and segment values

2.3.5 Double Words


A double word is exactly what its name implies, a pair of words. Therefore, a double word quantity
is 32 bits long as shown in Figure 2.5

Page 48 of 269
CSC 204: Fundamentals of Data Structures

Figure 2.5: Bit Numbers in a Double Word

Naturally, this double word can be divided into a high order word and a low order word, or four
different bytes, or eight different nibbles

Figure 2.6: Nibbles, Bytes, and Words in a Double Word

Pilot Question 2.3


i. Differentiate between nibbles and bytes as discussed in this study session.

2.4 Logical Operations on Bits


There are four main logical operations we’ll need to perform on hexadecimal and binary numbers:
AND, OR, XOR (exclusive-or), and NOT. Unlike the arithmetic operations, a hexadecimal
calculator isn’t necessary to perform these operations. It is often easier to do them by hand than to
use an electronic device to compute them.

2.4.1 The AND Operation


The logical AND operation is a dyadic operation (meaning it accepts exactly two operands). These
operands are single binary (base 2) bits. The AND operation is:

Page 49 of 269
CSC 204: Fundamentals of Data Structures

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

A compact way to represent the logical AND operation is with a truth table. A truth table takes the
following form:

Table 2.2: AND Truth Table

AND 0 1

0 0 0

1 0 1

The logical AND operation is, “If the first operand is one and the second operand is one, the result
is one; otherwise the result is zero.”

One important fact to note about the logical AND operation is that you can use it to force a zero
result. If one of the operands is zero, the result is always zero regardless of the other operand. In the
truth table above, for example, the row labelled with a zero input contains only zeros and the
column labelled with a zero only contains zero results.

Conversely, if one operand contains a one, the result is exactly the value of the second operand.
These features of the AND operation are very important, particularly when working with bit strings
and we want to force individual bits in the string to zero. We will investigate these uses of the
logical AND operation in the next section.

2.4.2 The OR Operation


The logical OR operation is also a dyadic operation. Its definition is given as follows:
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1

Page 50 of 269
CSC 204: Fundamentals of Data Structures

The truth table for the OR operation takes the following form:

Table 2.3: The OR Truth table

OR 0 1

0 0 1

1 1 1

Colloquially, the logical OR operation is, “If the first operand or the second operand (or both) is
one, the result is one; otherwise the result is zero.” This is also known as the inclusive-OR
operation.

If one of the operands to the logical-OR operation is one, the result is always one regardless of the
second operand’s value. If one operand is zero, the result is always the value of the second operand.
Like the logical AND operation, this is an important side-effect of the logical-OR operation that
will prove quite useful when working with bit strings.

2.4.3 The XOR Operation


The logical XOR (exclusive-OR) operation is also a dyadic operation. It is defined as follows:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

The truth table for the XOR operation takes the following form:

Table 2.4: The XOR Truth table


XOR 0 1

0 0 1

1 1 0

Page 51 of 269
CSC 204: Fundamentals of Data Structures

The logical XOR operation is, “If the first operand or the second operand, but not both, is one, the
result is one; otherwise the result is zero.” Note that the exclusive-or operation is closer to the
English meaning of the word “or” than is the logical OR operation.

If one of the operands to the logical exclusive-OR operation is a one, the result is always the
inverse of the other operand; that is, if one operand is one, the result is zero if the other operand is
one and the result is one if the other operand is zero. If the first operand contains a zero, then the
result is exactly the value of the second operand. This feature lets you selectively invert bits in a bit
string.

2.4.4 The NOT Operation


The logical NOT operation is a monadic operation (meaning it accepts only one operand). It is
defined as:

NOT 0 = 1

NOT 1 = 0

The truth table for the NOT operation takes the following form:

Table 2.5: The NOT Truth table

NOT 0 1

1 0

Page 52 of 269
CSC 204: Fundamentals of Data Structures

Summary of Study Session 2: Data Representation


In this Study Session 2, you have learnt that:

1. Data Representation refers to the methods used internally to represent information stored in
a computer.
2. A single hexadecimal digit consumes four binary digits (bits), and a group of four bits is
called a nibble
3. Groups of bits which are 8, 16, or 32 bits long. Objects of these sizes are called bytes,
words, and double words, respectively. With a byte, you can represent any one of 256
unique values.
4. With a word, you can represent one of 65,536 different values. With a double word, you can
represent over four billion different values.
5. There are four main logical operations we’ll need to perform on hexadecimal and binary
numbers: AND, OR, XOR (exclusive-or), and NOT. Unlike the arithmetic operations, a
hexadecimal calculator isn’t necessary to perform these operations.

Page 53 of 269
CSC 204: Fundamentals of Data Structures

Pilot Answers

Pilot Answer 2.1


i. Computers store information in form of a sequence of 0's and 1's.
ii. Data Representation.

Pilot Answer 2.2


i. The equivalent decimal values of these binary numbers are:
a. 110112 =
b. 101010102 =

Pilot Answer 2.3

i. A nibble is a collection of four (4) bits while a byte is a collection of eight (8) bits

Page 54 of 269
CSC 204: Fundamentals of Data Structures

Self-Assessment Questions (SAQs) for Study Session 2

Now that you have completed this study session, you can assess how well you have achieved its
Learning Outcomes by answering these questions. Write your answers in your Study Diary and
discuss them with your Tutor at the next Study Support Meeting. You can check your answers with
the Notes on the Self-Assessment Questions at the end of this Module.

i. Define data representation as discussed in this Study Session


ii. In what format does computer Store information?
iii. Convert the following binary numbers to decimal
a. 11012
b. 110011112
c. 101010012
iv. Define the following terms as discussed in this Study Session
a. Bits
b. Nibbles
c. Bytes
d. Words
e. Double Words
v. Draw the truth tables for the following logical Operations as explained in the Study
session

a. AND Operation
b. OR Operation
c. XOR operation
d. NOT operation

Page 55 of 269

You might also like