Expressions 9 5 2007 1 Opening Discussion Do you have any questions about the quiz Solutions to the interclass problem 2 Multiplication To multiply two binary numbers do standard long multiplication Just remember when you are adding that you are adding binary numbers Note that adding more than two binary numbers can get tricky It can result in carrying operations you have never done by hand in decimal 3 Binary Fractions If the digits in binary start with one and move to larger powers of two moving to the left what happens if you put in a binary point and move to the right Numbers that have a fixed number of bits with a point at a selected position are called fixed point numbers These were popular before floating point accelerators became common but now they are only used on specialty hardware with low end chips 4 Floating Point Numbers Numbers that can have fractional values are represented with floating point numbers on virtually all modern machines Floating point numbers are basically like scientific notation in binary v 1 s 1 frac 2 exp bias Single precision numbers use one bit for the sign 8 for the exponent 23 for the fractional part and have a bias of 127 Double precision uses one bit for sign 11 for exponent 52 for fraction and has a bias of 1023 5 Limitations of FP Floating point numbers are not actually the real numbers you know from math even though we often use them that way They have limitations because of the fixed number of bits Some of the math properties you are used to for numbers don t actually hold for floating point numbers Example a b c a b c Try this with a b and c as floats and make a 1e7 b 1e7 c 0 01 You also have to be careful of things like subtracting two numbers that are almost equal 6 Expressions in C Types of expressions Primary one operand no operator Postfix one operand followed by an operator sizeof type cast Binary Two operands with an operator between them Increment and decrement Unary one general operand preceded by an operator Function calls increment and decrement Prefix one operand must be a variable preceded by an operator Names literals and parentheses This is the most common form It is what you are used to from math but there are some interesting operators in C you won t see in math Includes assignment Ternary operator that takes three arguments We ll talk about this one later in the semester 7 Precedence and Associativity Different operators take precedence over others There is a table at the front of your book that lists precedence Some make sense before Some are C specific When in doubt put in parentheses to make it clear Associativity is the direction in which operators of the same precedence are considered 8 Side Effects Some expressions have side effects All expressions have a value associated with them I ll sometimes call it their return value An expression that has side effects not only returns a value it also changes something else in the memory of the machine Side effects are very important Good programming form doesn t place side effects in places where most people don t expect them Doing so makes code that is very hard for other people to read or debug Examples 9 Minute Essay Right now are you considering signing up for PAD2 next semester There is an ACM meeting this afternoon at 5 00 in HAS 329 You should consider coming if you are free Interclass Problem Write code that includes expressions involving five different operators Print the values of those expressions At least one of the operators must be non binary 10
View Full Document