16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 3: Number system basics Representing data in C
ECE Application Programming: Lecture 3 Lecture outline Announcements/reminders Course home page: http://mgeiger.eng.uml.edu/16216/f11/ Disc. gp.: http://groups.google.com/group/16216-fall-2011 Should have been invited All course announcements here Be careful when replying to e-mail! Assignment 1 due 11:59 PM today File names matter! Submit only .c file Assignment 2 coming Monday, due 9/19 Review: C program basics Today: representing data in C Number systems Variables and constants Will start operators (time permitting) 6/16/2018 ECE Application Programming: Lecture 3
Review: Basic C program structure Preprocessor directives #include: typically used to specify library files #define: generally used to define macros or constants Main program Starts with: int main() (or void main()) Enclosed in block: specified by { } Ends with return 0; Basic output Call printf(<string>); <string> can be replaced by characters enclosed in double quotes May include escape sequence, e.g. \n (new line) Comments Improve readability Multi-line: enclosed by /* */ Single line: starts with // , runs to end of line 6/16/2018 ECE Application Programming: Lecture 3
Number representation: bases Humans operate in decimal (base 10) Why don’t computers? Computers operate in binary (base 2) Each digit is a bit (binary digit) Can also use octal (base 8) or hexadecimal (base 16) Hex digits: 0-15, with A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 Base conversion Binary hex: start with LSB and make 4-bit groups e.g. 001 1011 01112 = 1B716 = 0x1B7 Note that an extra 0 is implied for the first group: 0010001 Binary decimal: multiply bit 0 by 20, bit 1 by 21, etc. e.g. 01112 = (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20) = = 0 + 4 + 2 + 1 = 710 Decimal binary / hex: repeated integer division, where remainder gives you each digit, starting with LSB 6/16/2018 ECE Application Programming: Lecture 3
Decimal binary / hex example 3510 = ?2 35 / 2 = 17 R1 (LSB) 17 / 2 = 8 R1 8 / 2 = 4 R0 4 / 2 = 2 R0 2 / 2 = 1 R0 1 / 2 = 0 R1 (MSB) 3510 = 1000112 3510 = ?16 35 / 16 = 2 R3 (LS digit) 2 / 16 = 0 R2 (MS digit) 3510 = 2316 Double-check: does this answer match the binary value we found? 0010 00112 2316 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Convert 2BAD|16 to Base 10 2 B A D |16 = ? |10 | | | | | | | `--- D x 160 = 13 x 1 = 13 | | `----- A x 161 = 10 x 16 = 160 | `------- B x 162 = 11 x 256 = 2816 `--------- 2 x 163 = 2 x 4096 = 8192 11181 Therefore, 2BAD|16 = 11181|10 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Convert 20DD|16 to Base 10 ?? 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Convert 20DD|16 to Base 10 2 0 D D |16 = ? |10 | | | | | | | `--- D x 160 = 13 x 1 = 13 | | `----- D x 161 = 13 x 16 = 208 | `------- 0 x 162 = 0 x 256 = 0 `--------- 2 x 163 = 2 x 4096 = 8192 8413 Therefore, 20DD|16 = 8413|10 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Convert FACE|16 to Base 10 ?? 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Convert FACE|16 to Base 10 F A C E |16 = ? |10 | | | | | | | `--- E x 160 = 14 x 1 = 14 | | `----- C x 161 = 12 x 16 = 192 | `------- A x 162 = 10 x 256 = 2560 `--------- F x 163 = 15 x 4096 = 61440 64206 Therefore, FACE|16 = 64206|10 6/16/2018 ECE Application Programming: Lecture 3
Base Conversions 10101110001101|2=?|16 ##10 1011 1000 1101 2 B 8 D Dec Bin Oct Hex 0000 00 1 0001 01 2 0010 02 3 0011 03 4 0100 04 5 0101 05 6 0110 06 7 0111 07 8 1000 10 9 1001 11 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F 10101110001101|2=?|16 ##10 1011 1000 1101 2 B 8 D 10101110001101|2=2B8D|16 NOTE: # is a place holder for zero Work from right to left Divide into 4 bit groups 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Base Conversions Dec Bin Oct Hex 0000 00 1 0001 01 2 0010 02 3 0011 03 4 0100 04 5 0101 05 6 0110 06 7 0111 07 8 1000 10 9 1001 11 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F FACE|16=?|2 F A C E 1111 1010 1100 1110 \ FACE|16=1111101011001110|2 6/16/2018 ECE Application Programming: Lecture 3
Example 1: Base conversions Perform the following base conversions 1110 = ?2 = ?16 3710 = ?2 = ?16 1116 = ?10 0x2F = ?2 = ?10 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Example 1 solution Perform the following base conversions 1110 = 10112 = B16 3710 = 1001012 = 2516 1116 = 1710 0x2F = 1011112 = 4710 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Representing data in C Two major questions (for now) What kind of data are we trying to represent? Data types Can the program change the data? Constants vs. variables 6/16/2018 ECE Application Programming: Lecture 3
Four Types of Basic Data Integer int Floating point (single precision) float Double Precision double Character char 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Integer Constants Any positive or negative number without a decimal point (or other illegal symbol). Legal values: 5 -10 +25 1000 253 -26351 +98 Illegal values: 2,523 (comma) 6.5 (decimal point) $59 (dollar sign) 5. (decimal point) 6/16/2018 ECE Application Programming: Lecture 3
Range of Integers (Machine Dependent) unsigned signed char 0 255 -128 +127 (8 bits) short int 0 65535 -32768 + 32767 short (16 bits) int 0 to 4294967295 -2147483648 2147483647 long long int (32 bits) 6/16/2018 ECE Application Programming: Lecture 3
float/double Constants Any signed or unsigned number with a decimal point Legal values: 5. .6 +2.7 0.0 -6.5 +8. 43.4 Legal (exponential notation): 1.624e3 7.32e-2 6.02e23 1.0e2 -4.23e2 +4.0e2 1.23e-4 +11.2e+7 Illegal: $54.23 6,349.70 1.0E5 6/16/2018 ECE Application Programming: Lecture 3
float/double Constants Range of float (32 bits) ± 1.175494351 E – 38 ± 3.402823466 E + 38 Range of double (64 bits) ± 2.2250738585072014 E – 308 ± 1.7976931348623158 E + 308 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Character Constants Stored in ASCII or UNICODE Signified by single quotes (’ ’) Valid character constants ’A’ ’B’ ’d’ ’z’ ’1’ ’2’ ’!’ ’+’ ’>’ ’?’ ’ ’ ’#’ Invalid character constants ’GEIGER’ ’\’ ’CR’ ’LF’ ’’’ ’’’’ ’”’ ”Q” 6/16/2018 ECE Application Programming: Lecture 3
Character Escape Sequences Meaning ’\b’ Backspace ’\’’ Single quote ’\n’ Newline ’\”’ Double quote ’\t’ Tab ’\nnn’ Char with octal value nnn ’\\’ Backslash ’\xnn’ Char with hex value nn 6/16/2018 ECE Application Programming: Lecture 3
Using #define with constants Often makes sense to give constant value a symbolic name for readability Don’t want constant wasting memory space Use #define to define a macro Macro: named code fragment; when name is used, the preprocessor replaces name with code Syntax: #define <name> <code> Common use: defining constants By convention, start constant values with capital letters e.g. #define NumberOne 1 At compile time, all uses of “NumberOne” in your program are replaced with “1” and then compiled 6/16/2018 ECE Application Programming: Lecture 3
ECE Application Programming: Lecture 3 Next time Variables Basic numeric output 6/16/2018 ECE Application Programming: Lecture 3