Two-State Design
Computer’s basic design depends on two states:
off and on (symbolized as 0 and 1) in the CPU,
FE1008 Computing RAM, storage devices, etc.
Chapter 3 Magnetic Disk
Fundamental Data Types
RAM
3-1 3-2
Binary Number System Examples
This 2-state design (off, on) corresponds to the 65 (decimal) = 1000001 (binary)
Binary Number System (base 2) in
mathematics. 65536 (dec) = 10000000000000000 (bin)
Binary digit or bit: 0 or 1 It is not necessary to know how to do this
0+1 = 1, 1+1 = 10 kind of conversion in this course.
10+1 = 11, 11+1 = 100 Use your scientific calculator to do
conversions, if necessary.
Consider the binary number:
1 0 1 0 0
3-3 3-4
Units of Data Storage Data Types: Integers
Binary digit or bit: 0 or 1 (bit) Data in computing are stored in various
forms known as data types.
Byte: 8 bits (bite)
C has the following fundamental data types:
1 Kilobyte (KB) = 1024 bytes = 210 bytes
char, int, float, double
1 Megabyte (MB) = 1024 KB = 220 bytes
1 Gigabyte (GB) = 1024 MB = 230 bytes Integers (int) are whole numbers (no
1 Terabyte (TB) = 1024 GB = 240 bytes decimal point). They are represented
exactly using bit patterns of 0s and 1s.
Petabyte, Exabyte, Zettabyte, Yottabyte
Example: 20 is represented exactly by 10100
3-5 3-6
Data Representation (integers)
1 bit can only represent 2 cases: 0, 1
Humans Computers
(numbers) (off, on) Hence we must use more bits to represent
various types of data.
0
If we use 2 bits, we get 4 possibilities:
1
00 01 10 11
2
3
For 3 bits: 000 001 010 011
4 100 101 110 111
5
There are 23 = 8 possibilities.
3-7 3-8
No of Bits Possible states All Possible Bit Patterns for 7 bits:
Exact Representation of Integers
4 16
6 64 0000000 (010)
7 128
0000001 (110)
8 256
16 65,536 0000010 (210)
32 4,294,967,296
. . .
1111110 (12610)
1111111 (12710)
3-9 3-10
Numbers: integers Characters in computing
26 x 2 letters (upper & lower case)
C provides several integer types, with
different storage requirements: 10 numeric digits,
short int: 16 bits = 2 bytes (-32768 to 32767) punctuation marks,
int: 32 bits = 4 bytes special characters (~ @ $ #, …)
long int: 32 bits = 4 bytes control characters, etc.
We also have unsigned short, unsigned int,
and unsigned long.
How are these stored in a computer?
Unsigned short: 2 bytes (0 to 65535)
Remember that computers can only deal with 0s
and 1s.
3-11 3-12
No of Bits Possible states Data Representation (Characters)
4 16 Humans Computers
6 64 (alphabets) (off, on)
7 128 A
8 256 B
16 65,536 C
D
E
Need at least 7 bits to represent characters in
computing. Normally use 8 bits (1 byte).
3-13 3-14
ASCII codes The ASCII Table
Each character has a numeric code called the 0 1 2 3 4 5 6 7 8 9
ASCII code. (Pronounced: askey). The 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT
computer stores this numeric code because it is 1 LF VT FF CR SO SI DLE DC1 DC2 DC3
not able to store the character shape that we 2 DC4 NAK SYN ETB CAN EM SUB ESC FS GS
use. 3 RS US SP ! “ # $ % & ‘
4 ( ) * + , - . / 0 1
ASCII = American Standard Code for 5 2 3 4 5 6 7 8 9 : ;
Information Interchange 6 < = > ? @ A B C D E
7 F G H I J K L M N O
8 P Q R S T U V W X Y
Char A B a b 0 1 + lf sp 9 Z [ \ ] ^ _ ' a b c
ASCII 65 66 97 98 48 49 43 10 32 10 d e f g h i j k l m
11 n o p q r s t u v w
lf = line feed, sp = space 12 x y z { | } ~ DEL
3-15 3-16
Upper ASCII & UNICODE Extended ASCII Codes
For the PC, there is also a set of upper (or
extended) ASCII codes, from 128 to 255
(so needs 8 bits). They represent European
characters and graphics characters.
CJK (Chinese, Japanese and Korean)
character sets need 16 bits – UNICODE
which includes characters/symbols from all
major languages in the world.
3-17 3-18
Character or number: Data Type: Characters
What is 1000001?
Declaration for a char variable: char ch;
We have seen that The computer will allocate a 1-byte cell (with
1000001 (binary) = 65 (dec) name ch) in RAM.
The bit string 1000001 represents both ‘A’ and
the integer 65. In fact, it may also represent an x memory cell
instruction or part of an instruction.
How does the computer know which is which? ch
The context, defined by the program, To store an actual characer such as the lower
determines the correct meaning. case x in ch, we write
ch = 'x'; //'x' is a character constant
3-19 3-20
Data Type: Characters Conversion Specifier
char ch = 'x'; (ASCII value = 120, or 11110002)
Conversion specifier: %c
printf("The character is %c", ch); printf("The character is %c", ch);
OUTPUT: OUTPUT:
The character is x The character is x
String constant: a list of characters. If we change %c to %d:
printf("The character is %d", ch);
Example: "FE1008 Computing"
the output is
The character is 120
3-21 3-22
Char data type and numeric Data Type: Integers
values
The char data type is also one of C’s integer
Type Meaning Value Range
data types. (Visual C++)
char character -128 to 127
Type Meaning Value Range short short integer -32,768 to 32,767
char character -128 to 127 int integer -2,147,483,648 to
2,147,483,647
unsigned long long integer Same as int
char 0 to 255 unsigned
char 0 to 255
unsigned
short 0 to 65535
3-23 3-24
Falling off the edge? The storage sizes of short, int, long are
system dependent.
Range for short int: -32,768 to 32,767
On some, short and int are the same: 2 bytes.
Suppose we have:
For Visual C++, int and long: 4 bytes.
short old_bal = 32767, new_bal; To find out the correct size of a data type on a
new_bal = old_bal + 1; system, use sizeof().
printf("The size of int is %d bytes.",
What do you think will happen if we execute sizeof(int));
printf("New balance = %d", new_bal); ?
Output (Visual C++):
Output: The size of int is 4 bytes.
New balance = -32768
3-25 3-26
Escape Sequences Example:
How do we output a double quote character " on
\n escape sequence, \ escape character
the screen?
Some common escape sequences are listed
below:
This is a double quote ".
Sequence Meaning It is wrong to write:
printf("This is a double quote ".");
\n newline
It should be
\t horizontal tab
printf("This is a double quote \".");
\0 null character
\" double quote Similarly, to output the backslash character, we
need to use the escape sequence \\.
\\ backslash 3-27 3-28
Floating-point Numbers The float Data Type
Various types: float, double, long double This representation gives the following range of
float type: 4 bytes = 32 bits values:
The IEEE Floating-Point Representation divides 1.2E-38 to 3.4E+38
the bits in the following way:
with a corresponding negative range.
Here, E-38 means 10-38.
Because of the limited number of bits allocated
Sign 8-bit
23-bit mantissa
for the mantissa (i.e., the fractional part), a
bit exponent number of the float type only has 6 or 7 digits
of precision (which is commonly referred to as
0 10000010 10110011000001000000000 single-precision).
3-29 3-30
Which real numbers can be Overflow & Underflow
represented exactly?
An example: When the result of a computation is greater than
the largest floating point value (about
0 10000001 01110000000000000000000
3.4E+38) , we get an overflow condition.
= 5.7500000 (decimal)
This usually indicates that something is wrong
The next larger number is: because 1038 is a huge number which is not
commonly met in applications.
0 10000001 01110000000000000000001
= 5.750000476837158203125 (decimal) If smaller than 1.2E-38, we get an underflow
condition which is usually not serious.
These two decimal numbers can be represented
exactly but there are infinitely many real For comparison, the mass of electron is about
numbers between these two numbers, and these 10-27 gm.
other numbers cannot be represented exactly.
3-31 3-32
The Real Line and Floats More accuracy & wider range
Double: 8 bytes = 64 bits
underflow overflow
Sign 11-bit 52-bit mantissa
0 bit exponent
FLT_MIN FLT_MAX Range: 2.2E-308 to 1.8E+308 (-ve range also)
= 1.2E-38 = 3.4E+38
Gives 14 or 15 digits of precision (double-
precision).
Note: FLT_MIN and FLT_MAX are macros It is the default data type for floating-point
defined in the system. numbers. A numeric constant with a decimal point
3-33 (such as 2.0) is considered as a double. 3-34
What ANSI C says Format/Conversion Specifiers
When outputting, we always need to specify what
ANSI C only guarantees that
and how to output. For example, in the
• a double is greater than or equal to a printf() below:
float, and
printf("The root is %f\n", root1);
• a long double is greater than or equal to
a double. the format string "The root is %f\n" is for
this purpose. %f is known as a conversion or
• For Visual C++, long double is the same as
format specifier.
double.
We have used conversion specifiers such as %d
and %c. There are several others.
3-35 3-36
Format/Conversion Specifiers Float type conversion specifiers
Character Argument Output
%f, %e, %E
%d integer signed decimal integer
Suppose number = 12.34
%f float signed floating point no.
%e float float using e notation %f gives 12.340000
%E float float using E notation %e gives 1.234000e+001
%c character a single character %E gives 1.234000E+001
%% % sign
Note that %e and %E are the same except for
the respective display of a lower case e and an
upper case E.
3-37 3-38
Float type format/conversion Format Modifiers
specifiers
We can specify:
1. A minimum field width
%f, %e
which is the number of spaces to use for output,
number = 0.0001234 e.g.
%f gives 0.000123 %5d (i.e. 5 spaces are to be used for
outputting an integer.)
%e gives 1.234000e-004 2. Number of decimal places (for floats)
%m.nf (m is the field width, n is the
number of decimal places)
Note: Use %e if you are not sure about the size
of numbers to be output. 3. Left justification
Default is right justification.
3-39 3-40
Format Modifiers Outputting the % sign
Examples:
To output the percentage sign %, we need to
(1) To output 123
type two % signs:
(a) using %5d gives [ ][ ]123 (right justification)
(b) using %-5d gives 123[ ][ ] (left justification) printf("This is the %% sign.");
(2) Suppose the number is 12.34 Note: Just like the backslash (\) character,
%8.3f ________ % also has a special meaning in the
printf() statement. As you have seen, it is
(A total of 8 space are to be used)
used in the form %c, %d, %f, etc.
Try %1.3f %0.3f %.3f (If the field
width is too small, the computer will ignore it.)
See Hands-on 3 (printf.c)
3-41 3-42