CSC 204 Session 2
CSC 204 Session 2
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.
Page 39 of 269
CSC 204: Fundamentals of Data Structures
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.
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.
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.
Page 41 of 269
CSC 204: Fundamentals of Data Structures
= 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.
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”
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:
OR
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:
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.
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:
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
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
Although computers usually provide instructions that can test and manipulate bits, they generally
are designed to store data and execute instructions in bit multiples
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
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
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
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
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
Page 48 of 269
CSC 204: Fundamentals of Data Structures
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
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:
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.
Page 50 of 269
CSC 204: Fundamentals of Data Structures
The truth table for the OR operation takes the following form:
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.
The truth table for the XOR operation takes the following form:
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.
NOT 0 = 1
NOT 1 = 0
The truth table for the NOT operation takes the following form:
NOT 0 1
1 0
Page 52 of 269
CSC 204: Fundamentals of Data Structures
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
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
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.
a. AND Operation
b. OR Operation
c. XOR operation
d. NOT operation
Page 55 of 269