Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2011 Lecture 3: Number system basics Representing data in C

2 ECE Application Programming: Lecture 3
Lecture outline Announcements/reminders Course home page: Disc. gp.: Should have been invited All course announcements here Be careful when replying to ! 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

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

4 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 = 1B716 = 0x1B7 Note that an extra 0 is implied for the first group: 0010001 Binary  decimal: multiply bit 0 by 20, bit 1 by 21, etc. e.g = (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20) = = = 710 Decimal  binary / hex: repeated integer division, where remainder gives you each digit, starting with LSB 6/16/2018 ECE Application Programming: Lecture 3

5 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 = 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?  2316 6/16/2018 ECE Application Programming: Lecture 3

6 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 ` x 163 = 2 x 4096 = Therefore, 2BAD|16 = 11181|10 6/16/2018 ECE Application Programming: Lecture 3

7 ECE Application Programming: Lecture 3
Base Conversions Convert 20DD|16 to Base 10 ?? 6/16/2018 ECE Application Programming: Lecture 3

8 ECE Application Programming: Lecture 3
Base Conversions Convert 20DD|16 to Base D D |16 = ? |10 | | | | | | | `--- D x 160 = 13 x 1 = 13 | | `----- D x 161 = 13 x 16 = 208 | ` x 162 = 0 x 256 = 0 ` x 163 = 2 x 4096 = Therefore, 20DD|16 = 8413|10 6/16/2018 ECE Application Programming: Lecture 3

9 ECE Application Programming: Lecture 3
Base Conversions Convert FACE|16 to Base 10 ?? 6/16/2018 ECE Application Programming: Lecture 3

10 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 = Therefore, FACE|16 = 64206|10 6/16/2018 ECE Application Programming: Lecture 3

11 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 |2=?|16 ## 2 B D |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

12 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 \ FACE|16= |2 6/16/2018 ECE Application Programming: Lecture 3

13 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

14 ECE Application Programming: Lecture 3
Example 1 solution Perform the following base conversions 1110 = = B16 3710 = = 2516 1116 = 1710 0x2F = = 4710 6/16/2018 ECE Application Programming: Lecture 3

15 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

16 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

17 ECE Application Programming: Lecture 3
Integer Constants Any positive or negative number without a decimal point (or other illegal symbol). Legal values: Illegal values: 2,523 (comma) 6.5 (decimal point) $59 (dollar sign) 5. (decimal point) 6/16/2018 ECE Application Programming: Lecture 3

18 Range of Integers (Machine Dependent)
unsigned signed char 0   +127 (8 bits) short int 0   short (16 bits) int 0 to  long long int (32 bits) 6/16/2018 ECE Application Programming: Lecture 3

19 float/double Constants
Any signed or unsigned number with a decimal point Legal values: Legal (exponential notation): 1.624e e e23 1.0e e e e e+7 Illegal: $ , E5 6/16/2018 ECE Application Programming: Lecture 3

20 float/double Constants
Range of float (32 bits) ± E – 38 ± E + 38 Range of double (64 bits) ± E – 308 ± E + 308 6/16/2018 ECE Application Programming: Lecture 3

21 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

22 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

23 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

24 ECE Application Programming: Lecture 3
Next time Variables Basic numeric output 6/16/2018 ECE Application Programming: Lecture 3


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google