CS173: Intro to Computer Science - Discrete Data Ranges
Activity Goals
The goals of this activity are:- To explain that data types are discrete because they are represented by a finite number of computing bits
- To explain that all data are represented computationally via binary values
- To explain the relationship between a data type's bit size and its range
- To explain why typed values take on discrete ranged values that can differ from data type to data type
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity on the Class Activity Questions discussion board. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Data Types are Discrete
Type Name
Number of Bits
Number of Values that Can Be Stored
boolean
1 bit
2
int
32 bits = 4 bytes
\(2^{32}\) = approximately 4 billion values
Each additional bit doubles the number of values that can be stored: all the possible values with a
Each additional bit doubles the number of values that can be stored: all the possible values with a
1
appended before them, plus all the possible values with a 0
appended before them!
double
64 bits = 8 bytes
\(2^{64}\) = approximately \(18 \times 10^{18}\) values
char
8 bits = 1 byte
\(2^{8}\) = approximately 256 values
String
1 byte per character (plus one additional byte at the end which contains the value
0
)
Each string is stored in memory with enough space to store all the characters plus the extra byte at the end. In a way, a string holds only one value, made up of its characters. We call strings immutable because we must create a new string to hold new textual data, rather than simply edit the original string variable.
Questions
- Why can’t a computer store data with an infinite number of possible values?
- A
boolean
can hold only two values, because its single bit is either a1
or a0
. To what do these values correspond? - The
float
data type is a smaller version of thedouble
; how many bits do you think afloat
uses? - Similarly, there is a larger version of the
int
data type called thelong
. How many bits do you think this type uses, and how many values can it contain? - Which of the data types above has a variable rather than fixed length? With this in mind, what do you think is the purpose of storing the extra
0
at the end of values of that type?
Model 2: Data Types Have Ranges Because They Are Discrete
Type Name
Number of Bits
Number of Values
Range
boolean
1 bit
2
0
or 1
int
32 bits = 4 bytes
\(2^{32}\)
-2,147,483,648
through 2,147,483,647
double
64 bits = 8 bytes
\(2^{64}\)
Approximately \(-1.797 \times 10^{308}\) through \(1.797 \times 10^{308}\), with approximately 15 significant digits of precision
Questions
- The largest value a
boolean
can store is1
, even though it is a single bit value that can hold two discrete values. This is because a bit is needed to store the value0
. So aboolean
’s largest value is not \(2^{1}\), but actually \(2^{1} - 1\). What is the largest int value that a computer can store? - Why can the int data type store more negative values than positive values?
- Suppose you required more precision than the 15 significant digits offered by the double type. What would happen to the range if a double could provide more precision, and why?
Model 3: Larger Data Types Can Store More Values (Larger Values and More Precise Values)
Type Name
Number of Bits
Number of Values
Range
int
32 bits = 4 bytes
\(2^{32}\)
-2,147,483,648
through 2,147,483,647
long
64 bits = 8 bytes
???
???
float
32 bits = 4 bytes
???
Approximately \(-3.402 \times 10^{38}\) through \(3.402 \times 10^{38}\), with approximately 6 significant digits of precision
double
64 bits = 8 bytes
\(2^{64}\)
Approximately \(-1.797 \times 10^{308}\) through \(1.797 \times 10^{308}\), with approximately 15 significant digits of precision
Questions
- Fill in the blanks in the table above.
Reflective Journal Prompt
- Numeric data types must hold not only positive values but also negative values. What does this do to the number of positive values that can be represented by each type? What are their ranges?