Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Scala, Binary, Machine Arithmetic1-21-2011Opening DiscussionMinute Essay commentsCopy/paste with middle click on Linux. On Windows?vi color-coding.Never touch the mouse.Low learning curve in Scala.Case sensitivityGoing to the web for help. GoogleKey TermsToken – A set of characters that has meaning to the language. (Words)Statement – A set of tokens that give a complete instruction. (Sentences)Expression – Tokens put together that produce a value. (Phrases)Type – All values have types. A type is a set of values and the operations allowed on them.Literal – A token that represents a value.Numeric, String, Character, BooleanStatements and Semicolon InferenceIn Scala, as with most programming languages, programs are made by putting together statements.In Scala, any expression is a valid statement as are a few other constructs.Statements end with semicolons, but they will be inferred at the end of a line if they make sense so you rarely type them.OperatorsWe can build longer expressions by putting literals together with operators.Let's start off by playing with some of the numeric operations you are probably familiar with.+, -, *, /You can get the remainder after division with %.ObjectsAn object is defined to be information along with the things you can do with that information.The information in an object is called the properties.The actions are called methods.In Scala, even things like Int are objects and have methods on them.MethodsThe normal way to call a method in Scala (and most other object-oriented languages) is to put a period after the object and follow it with the method name.The REPL will do tab completion and list methods for you.Let's look at the methods on some basic types and try calling them.ArgumentsSome methods need additional information to work.To give this to the method we pass in arguments.Arguments are put in parentheses and separated by commas if there is more than one.The parentheses are generally optional in Scala if there is no argument.Operator SyntaxAll the “operators” in Scala are really just methods.Scala allows any method with zero or one arguments to be called with an operator syntax.That means you leave off the dot and the parentheses.If a method takes no arguments you can call it without the dot.Bases and BinaryThe decimal numbers we use are base 10. Each digit to the left is a higher power of 10.There is nothing special with decimal (other than perhaps we have 10 fingers). Other bases are equally valid.Computers use binary numbers to store everything.All digits are 0 or 1 and each position is a higher power of 2.toBinaryStringBinary AdditionAdding binary numbers is very easy. Just do the long addition that you are used to.You will carry a lot more frequently because anything above 1 causes a carry.Let's run through some examples.Consider implications of fixed precision.Negative NumbersWe don't have a – in the computer for negative numbers. All we have are 1 and 0. So how do we make negative numbers?Remember the definition of negative numbers as additive inverse.a+(-a)=0We want to preserve this to keep addition simple.This gives us 2s-compliment numbers.Binary MultiplicationMultiplying binary numbers works just like long multiplication with decimals, but easier.My only recommendation is you only add two numbers at a time and take it in steps.HexadecimalBinary is unwieldy for humans because of the large number of digits.Hexadecimal (base 16) is commonly used because it converts nicely to binary, but has few digits.Four bits is a hex digit. Start at the right and group bits by 4.Use letters A-F for numbers 10-15.Hex literals start with 0xtoHexStringOctalOctal (base 8) is less common than hex, but not uncommon.Group bits into groups of three.Octal literals and toOctalString().The math ObjectFor other math functions use methods on the math object.For example, use math.sqrt() to take the square root of a number.Minute EssayConvert 276 to binary.We have our first quiz next
View Full Document