Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance.

Similar presentations


Presentation on theme: " 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance."— Presentation transcript:

1  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance –Binary Binary two's complement negative representation –Octal –Hexadecimal Macros Types The format-control specifiers for the function printf()

2  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System All data is stored as binary in a computer –Data is stored as a series of high and low voltages on a computer. –Transistors and logic gates enable arithmetic and logical operations to be performed on binary data.

3  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System Simple Binary Arithmetic –addition –multiplication –complement

4  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Conversion from binary to decimal

5  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System Negative Number Representation –Different methods exist sign-and-magnitude Binary two's complement –Which method is better?

6  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Octal Number System What is the octal number system? Why do we use the octal number system? –Conversion to and from binary to octal –Common concepts represented in octal

7  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Hexadecimal Number System What is the hexadecimal system? What is the hexadecimal system used for? –Memory Addresses –MAC Addresses –WEP Wireless Keys Conversion to and from binary to hexadecimal

8  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Base Conversions for Integers Find the decimal, octal, and hexadecimal values of the following binary numbers. –0b00110011 51 (decimal), 063 (octal), 0x33 (hexadecimal)‏ –0b11111011 (two's complement notation)‏ -5 (decimal), 0373, 0xFB

9  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Binary Representation for Integers Find the binary values of the following integers using 8 bits. –28 0b00011100 –-28 0b11100100

10  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What is a macro? How does a macro work? What do we use macros for and why? Some final tips regarding macros

11  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What is a macro? –A macro is a “shortcut”. It allows a programmer to “macro” a number or variable. #define M_PI 3.14159 #define NUM_PPL 54

12  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros How does a macro work? –When a C program is executed, the execution engine will literally replace all occurrences of a macro with its defined value. #include #define DEFAULT_NUM 5 int main() { printf(“My default num is %d.\n”, DEFAULT_NUM); return 0; }

13  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What do we use macros for and why? –To avoid “magic numbers” A magic number is a numeric constant that appears in code with no explanation. –To replace long variable names or numbers by meaningful shorter macros that are easier to type, dead, and change. Easy to develop and maintain a program.

14  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros Some final considerations –Macros are generally named in capital letters. –Macro names should be meaningful. –Macros cannot be changed during program execution, unlike normal variables which may change value during the execution.

15  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Types Variables in C must be “declared” before they may be used. Each variable in C is a certain “type”. Different types of variables are useful for different things, or can hold a different amount of information.

16  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The format specifiers for printf() “%b” in Ch onlybinary number “%uhd”unsigned short “%ulld”unsigned long long “%u”unsigned int “%hd”short “%lf”double “%s”string “%f”float “%p”pointer “%lld”long “%d”int “%c”char Format-Control-StringArgument Type

17  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The precision and field width for printf() specifiers Additional information may be provided to the specifiers to further refine output. %.10lf ← This tells printf() to print a double with 10 digits after the decimal point. %20.5lf ← this tells printf() to print a double that takes up to 20 character spaces. The left side of the number is padded with spaces to make up the difference.

18  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach printf() specifiers > printf("%20lf\n", 5.4) 5.400000 > printf("%5.10lf\n", 5.4) 5.4000000000 > printf("%.10lf\n", 5.4); 5.4000000000 > printf("%15.10lf\n", 5.4); 5.4000000000 > printf("%8.2lf\n", 5.4); 5.40


Download ppt " 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance."

Similar presentations


Ads by Google