ECE Application Programming

Slides:



Advertisements
Similar presentations
CMT Programming Software Applications
Advertisements

Chapter 2 Data Types, Declarations, and Displays
Simple Data Type Representation and conversion of numbers
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
C-Language Keywords(C99)
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Chapter 2 Variables.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Data Representation COE 308 Computer Architecture
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 2 Variables.
CSE 220 – C Programming Bitwise Operators.
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
ECE Application Programming
Java Variables and Types
ECE Application Programming
ECE Application Programming
Microprocessor Systems Design I
Tokens in C Keywords Identifiers Constants
Wel come.
7. BASIC TYPES.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
EPSII 59:006 Spring 2004.
ECE Application Programming
Chapter 2: Introduction to C++
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 Variables.
Lectures on Numerical Methods
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Data Representation ICS 233
WEEK-2.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Unit 3: Variables in Java
Chapter 2 Variables.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
The Fundamentals of C++
EECE.2160 ECE Application Programming
Data Representation COE 308 Computer Architecture
EECE.2160 ECE Application Programming
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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: 0010001 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