Sun, 5 Sep 2021, 19:25 Data Types



My students,

My TAs tell me that several of you have questions about data types.  It is
the central notion of Chapter 2 of the textbook, and is in my view essential
to your understanding.  Thus I will spend some time on it later this evening
during my special class at 8pm.  I know some of you can not attend class this
evening, so I will try to explain it in this email.  Best of course is to be
in class where you can interrupt me to ask questions or after class when we
continue to meet to answer your questions.  But if you can't, you can't.  You
will have the recording of my lecture and this email.  AND I and my TAs are
always available to answer questions.  Just send me email and we will set up
a time to meet.

Now, onto DATA TYPES.

As I have stated several times already: A DATA TYPE is a representation of
information such that the ISA of the computer has instructions that operate
on that data type.

What does that mean?

First, there are many ways to represent the same piece of information.  Take
the number 6, for example.  We can represent it as: 6.  Good for human 
interaction, since it is easy to recognize without any computation.  Or, as
a 2's complement integer: 000..00110.  Good for the computer when it is adding
numbers.  Or, as 00110110, the ASCII code for 6, good for moving a value typed
on a standard keyboard into the computer, or displaying a value on a standard
monitor.  We can also represent the number 6 as 111111, or as a FLOATING POINT 
data type which I will get to later. 

There are also DATA TYPES that have nothing to do with the value 6.  ...such as
logigal variables, where a variable can have one of only two possible values, 
TRUE and FALSE, generally represented as 1 or 0. 

There are other data types that have nothing to do with numbers, and we will
get to that much later in the semester.  For right now, it is enough to 
recognize that a number can be represented in many ways.

What makes a representation a data type is having instructions in the ISA that
operate on that representation.  It is not necessary (and in fact is never the
case) that any instruction operates on all data types.  But there must be some
instructions that operate on a representation in order for the representation 
to be called a data type.

In fact, and very important: If the instruction is to process successfully, 
the operands needed for that instruction MUST be in the corresponding 
representation.

To add two integers in the computer, almost all ISAs require the integers to
be in the data type: 2's complement integer.  If the integers are in a 
different data type, the instruction will create garbage.  

My favorite example of creating garbage by having an instruction require one
data type but the values being added are in a different data type is trying to
add two numbers that are in the data type ASCII code.  ASCII code data type
is for input and output.  Addition requires 2's complement data type.  The
mismatch creates garbage.  Suppose I add the ASCII code for 2 (00110010) to 
the ASCII code for 3 (00110011).  What is the result?  Answer: 01100101, which
is the ascii code for lower case e.  Does 2 + 3 = e?  Of course not.  The 
problem is we used an instruction ADD that requires its operands to be
2's complement integers, and provided that instruction with operands that were
of the ASCII data type.

Another data type is the logical variable, where the value of each variable
is 0 or 1 (originally, and often TRUE or FALSE).  For this to be a data type
of an ISA, the ISA has to have instructions that operate on it.  In class we
mentioned three possible operations: NOT, AND, OR.  In order to perform NOT, 
AND, or OR on values, those values have to be 0 or 1.  No 3's or 4's, for 
example: just 0 or 1.

For convenience, we often collect a number of logical variables that we want
to operate on with a single NOT, AND, or OR instruction.  Each logical variable
is independent of the others so I can perform the NOT, AND, or OR simultaneously
on all the logical variables in the collection.  We refer to the collection as
a bit vector, and the operation (NOT, AND, or OR) as performing the operation
on the collection independently and simultaneously.  We use the term bit-wise
to sepcify that independence and simultaneity.

You know that the computer's operation is specified by its ISA, and each 
instruction in the ISA assumes that the source operands for an instruction 
are in the appropriate data type.  The computer has no way of knowing what 
the programmer did, so if the programmer is provided operands in a different 
data type, the computer will assume that the programmer provided the 
correct data type.  Our example adding ascii 2 to ascii 3 is a good 
example of that.  The programmer provided an ascii 2, which is 00110010,
and an ascii 3 which is 00110011.  The computer assumed that since the 
programmer asked it to ADD, 00110010 and 00110011 must be  2's complement 
integers.  i.e. 00110010 is 50 and 00110011 is 51.  So the computer adds them
yielding 101, which is the ascii code 01100101 for lower case e.  That is, the
computer did what you told it to do, not what you wanted it to do.

This illustrates one final notion before I ship this to you.  We have discussed
above that a value can be expressed according to many different data types.
It is also the case that a single sequence of 0s and 1s can represent many
different things depending on what data type is being represented.  We have all
had many experiences of that in our lives already.  When you are driving and 
see a red light, the red light means stop.  At a hockey game, the red light
that goes on above the net means someone just scored a goal,   When you are
walking in the red light district of Frankfurt, Germany and see a red light in
the window, it means something else!  The same red light, but three very
different meanings.  The same thing is true of bit patterns in the computer.
The same bit pattern in the computer can be interpreted many different ways,
depending on how the computer is being asked to use it.

Sorry for being so long-winded, I hope it makes things clearer.  If there are
any questions, please let me or my TAs know.

Good luck with EE306.  ...and stay safe.

Yale Patt