EPSII 59:006 Spring 2004
Numbers in Computers Why use data types? Number systems - binary, octal, decimal, hexadecimal Data representation - bits, bytes, words (of different sizes) Types Integer Char Float, Double Signed, Unsigned
Data Types Variables are names for places in memory There are different types of variables Different variables require different amounts of information to store them In C the main types are: char, int, float, double
How the Computer Stores Variables Computers must code each variable into series of electronic “on” and “off” switches. Information (both instructions and data) are represented by binary codes (1’s and O’s).
Binary Numbers 1111 == 15 1000 0000 == 128 1111 1111 == 255 0 == 0 10 == 2 11 == 3 100 == 4 101== 5 110 == 6 111 == 7 1000 == 8 1111 == 15 1000 0000 == 128 1111 1111 == 255
Breaking It Down! Binary: a3*23 + a2*22 + a1*21 + a0*20 1001 = 1*23 + 0*22 + 0*21 + 1*20 1001 = 1*8 + 0*4 + 0*2 + 1*1 1001 = 8+1 1001 = 9 Decimal: a3 a2 a1 a0 = a3 * 103 + a2*102 + a1*101 + a0*100
Question What does 0001 1101 Equal in decimal?
Solution 0001 1101 = 16 + 8 + 4 + 1 = 29
Hexadecimal Binary is a pain to work with because the representation is so long There are 16 possible combination of 4 binary digits
Hexadecimal Code 0000 == 0 == 0 0001 == 1 == 1 0010 == 2 == 2 0011 == 3 == 3 0100 == 4 == 4 0101 == 5 == 5 0110 == 6 == 6 0111 == 7 == 7 1000 == 8 == 8 1001 == 9 == 9 1010 == 10 == A 1011 == 11 == B 1100 == 12 == C 1101 == 13 == D 1110 == 14 == E 1111 == 15 == F
Question What is the octal code?
Answer a3a2a1a0 Octal = a3*83 + a2*82 + a1*81 + a0*80
Exercise Convert A9 to binary Convert 1101 1100 to Hex
Solution Convert A9 to binary 1010 1001 Convert 1101 1100 to Hex D C
How Do All These Bits Get Organized? byte n. - A sequence of adjacent bits, usually eight, operated on as a unit by a computer. word n. - Computer Science. A set of bits constituting the primary unit of addressable memory. 4 bits = nibble = 16 states (24) 8 bits = byte = 256 states (28) 16 bits = word = 65536 states (216) => 64K Memory is arranged in bytes – each individual address points to the beginning of a byte.
How are variables stored? Characters are stored as numbers: ASCII : ftp://dkuug.dk/i18n/WG15-collection/charmaps/ANSI_X3.4-1968 Unicode over 65000 characters – every written language. Integers stored as binary Floating point numbers are stored with limited precision according to the IEEE standard. Double precision floats (type double) are preferred for numerical calculations.
Standard Variable Type Storage Used For Storage (bytes) int Integers 4 (usually, but is really implementation-dependent) char Characters 1 float Real numbers 4 double 8
Other Variable Types Type unsigned positive Integers 4 (usually, but is really implementation-dependent) short integers 2 long 4 (usually)
Variable Type Storage Type Storage (bytes) Range of Values int 4 char 1 -128 to 127 float -1.17549435e-38 to 3.40282347e+38 double 8 -2.2250738585072014e-308 to 1.7976931348623157e+308
Finding variable sizes for your computer Use the sizeof() function: printf(“An int is %d bytes ”,sizeof(int)); printf(“and a char is %d bytes\n”,sizeof(char));