Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Scala, Binary, Machine Arithmetic9-2-2011Opening DiscussionCS majors e-mail list.Minute essay's online.Minute Essay commentsVariables (we will get there)Adding two characters?Editing in Windows 7, use Notepad.You don't have to install Scala on your computer if you always do a remote connection.We will use the REPL to show little things and write scripts to really put stuff together.More Minute EssaysWhen to use a Char.You want to stay on top of things. We keep building.We are covering a tiny fraction of the possibilities. Unix Power Tools.Capabilities of Scala, comparison to Java.Getting out of things.Repeat vi commands.Math constants in Scala.Moving fast.IcP solutions.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.Enjoy your 3-day weekend.We have our first quiz next
View Full Document