CS173: Intro to Computer Science - Discrete Data Ranges

Activity Goals

The goals of this activity are:
  1. To explain that data types are discrete because they are represented by a finite number of computing bits
  2. To explain that all data are represented computationally via binary values
  3. To explain the relationship between a data type's bit size and its range
  4. 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 take notes for the group, and appoint another member to discuss your findings with the class. After class, think about the questions in the reflective prompt and respond to those individually. 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 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

  1. Why can’t a computer store data with an infinite number of possible values?
  2. A boolean can hold only two values, because its single bit is either a 1 or a 0. To what do these values correspond?
  3. The float data type is a smaller version of the double; how many bits do you think a float uses?
  4. Similarly, there is a larger version of the int data type called the long. How many bits do you think this type uses, and how many values can it contain?
  5. 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

  1. The largest value a boolean can store is 1, even though it is a single bit value that can hold two discrete values. This is because a bit is needed to store the value 0. So a boolean’s largest value is not \(2^{1}\), but actually \(2^{1} - 1\). What is the largest int value that a computer can store?
  2. Why can the int data type store more negative values than positive values?
  3. 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

  1. Fill in the blanks in the table above.

Reflective Journal Prompt

  1. 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?

Submission

Submit your answers to the questions using the Collaborative Spaces section of OneNote. You can add a page with your name and your group members' names, and today's date, as the title. Under the appropriate section (i.e., "Class Notes", "Collaborative Spaces", "Reflective Prompts") that you can select on the left side of the screen, you can click "Add Page" on the right side. You can answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section.