cs222

36
CS222 Week 2 - Wednesday

Upload: karl

Post on 06-Feb-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Week 2 - Wednesday. CS222. Last time. What did we talk about last time? C compilation model Lab 1. Questions?. Project 1. Quotes. Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two. Buddha. Base 10 (decimal) numbers. - PowerPoint PPT Presentation

TRANSCRIPT

CS121

CS222Week 2 - WednesdayLast timeWhat did we talk about last time?C compilation modelLab 1Questions?Project 1QuotesUnity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.

BuddhaMakefilesThe order of compilation matters You have to compile all necessary files yourself to make your program workTo make these issues easier to deal with, the make utility is usedThis utility uses makefilesEach makefile has a list of targetsEach target is followed by a colon and a list of dependenciesAfter the list of dependencies, on a new line, preceded by a tab, is the command needed to create the target from the dependenciesSample makefileMakefiles are called makefile or Makefileall: hello

hello: hello.cgcc -o hello hello.c

clean:rm -f *.o helloBase 10 (decimal) numbersOur normal number system is base 10This means that our digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9Base 10 means that you need 2 digits to represent ten, namely 1 and 0Each place in the number as you move left corresponds to an increase by a factor of 10Base 10 Example3,482,931OnesMillionsHundredsThousandsTensHundredthousandsTen thousandsBase 2 (binary) numbersThe binary number system is base 2This means that its digits are: 0 and 1Base 2 means that you need 2 digits to represent two, namely 1 and 0Each place in the number as you move left corresponds to an increase by a factor of 2 instead of 10Base 2 Example11111100000Ones1024sSixteensThirty twosEightsSixty foursTwosFours256s128s512s

C LiteralsLiteralsBy default, every integer is assumed to be a signed intIf you want to mark a literal as long, put an L or an l at the endlong value = 2L;Don't use l, it looks too much like 1There's no way to mark a literal as a shortIf you want to mark it unsigned, you can use a U or a uunsigned int x = 500u;Every value with a decimal point is assumed to be doubleIf you want to mark it as a float, put an f or an F at the endfloat z = 1.0f;Integers in other basesYou can also write a literal in hexadecimal or octalA hexadecimal literal begins with 0xint a = 0xDEADBEEF;Hexadecimal digits are 0 9 and A F (upper or lower case)An octal literal begins with 0int b = 0765;Octal digits are 0 7Be careful not to prepend other numbers with 0, because they will be in octal!Remember, this changes only how you write the literal, not how it is stored in the computerCan't write binary literalsPrinting in other basesThe printf() function provides flags for printing out integers in:%dDecimal%xHexadecimal (%X will print A-F in uppercase)%oOctalprintf("%d", 1050); //prints 1050printf("%x", 1050); //prints 41aprintf("%o", 1050); //prints 2032

Binary representationThis system works fine for unsigned integer valuesHowever many bits you've got, take the pattern of 1's and 0's and convert to decimalWhat about signed integers that are negative?Most modern hardware (and consequently C and Java) use two's complement representationTwo's complementTwo's complement only makes sense for a representation with a fixed number of bitsBut we can use it for any fixed numberIf the most significant bit (MSB) is a 1, the number is negativeOtherwise, it's positiveUnfortunately, it's not as simple as flipping the MSB to change signsNegative integer in two's complementLet's say you have a positive number n and want the representation of n in two's complement with k bitsFigure out the pattern of k 0's and 1's for nFlip every single bit in that pattern (changing all 0's to 1's and all 1's to 0's)This is called one's complementThen, add 1 to the final representation as if it were positive, carrying the value if neededExampleFor simplicity, let's use 4-bit, two's complementFind -66 is 0110Flipped is 1001Adding 1 gives 1010Two's complement to negative integerLet's say you have a k bits representation of a negative number and want to know what it isSubtract 1 from the representation, borrowing if neededFlip every single bit that pattern (changing all 0's to 1's and all 1's to 0's)Determine the final integer valueExampleFor simplicity, let's use 4-bit, two's complementGiven 1110Subtracting 11101Flipped is 0010Which is 2, meaning that the value is -2All four bit numbersBinaryDecimalBinaryDecimal000001000-8000111001-7001021010-6001131011-5010041100-4010151101-3011061110-2011171111-1But why?!Using the flipping system makes it so that adding negative and positive numbers can be done without any conversionExample 5 + -3 = 0101 + 1101 = 0010 = 2Overflow doesn't matterTwo's complement (adding the 1 to the representation) is needed for this to workIt preserves parity for negative numbersIt keeps us with a single representation for zeroWe end up with one extra negative number than positive numberFloating point representationOkay, how do we represent floating point numbers?A completely different system!IEEE-754 standardOne bit is the sign bitThen some bits are for the exponent (8 bits for float, 11 bits for double)Then some bits are for the mantissa (23 bits for float, 52 bits for double)More complexityExcept even that isn't enough!How would you represent zero?If all the bits are zero, the number is 0.0There are other special casesIf every bit of the exponent is set (but all of the mantissa is zeroes), the value is positive or negative infinityIf every bit of the exponent is set (and some of the mantissa bits are set), the value is positive or negative NaN (not a number)

NumberRepresentation0.00x000000001.00x3F8000000.50x3F0000003.00x40400000+Infinity0x7F800000-Infinity0xFF800000+NaN0x7FC00000and othersOne little endianFor both integers and floating-point values, the most significant bit determines the signBut is that bit on the rightmost side or the leftmost side?What does left or right even mean inside a computer?The property is the endianness of a computerSome computers store the most significant bit first in the representation of a numberThese are called big-endian machinesOthers store the least significant bit firstThese are called little-endian machines

Why does it matter?Usually, it doesn't!It's all internally consistentC uses the appropriate endianness of the machineWith pointers, you can look at each byte inside of an int (or other type) in orderWhen doing that, endianness affects the byte orderingThe term is also applied to things outside of memory addressesMixed-endian is rare for memory, but possible in other cases:http://users.etown.edu/w/wittmanb/cs222/More specificMore specificMath libraryFunctionResultFunctionResultcos(double theta)Cosineof thetaexp(double x) exsin(double theta)Sine of thetalog(double x)Natural logarithm of xtan(double theta)Tangent of thetalog10(double x)Common logarithmof xacos(double x) Arc cosineof xpow(double base, double exponent)Raise base to power exponentasin(double x) Arc sineof xsqrt(double x) Square root of xatan(double x) Arc tangent of xceil(double x)Round up valueof xatan2(double y, double x) Arc tangent of y/xfloor(double x)Round down valueof xfabs(double x) Absolute valueof xfmod(double value, double divisor)Remainder of dividing value by divisorMath library in actionYou must #include to use math functions#include #include

int main(){double a = 3.0;double b = 4.0;double c = sqrt(a*a + b*c);printf("Hypotenuse: %f\n", c); return 0;}It doesn't work!Just using #include gives the headers for math functions, not the actual codeYou must link the math library with flag lm

Now, how are you supposed to know that?> gcc -lm hypotenuse.c -o hypotenuse> man 3 sqrtMy main manMan (manual) pages give you more information about commands and functions, in 8 areas:General commandsSystem callsLibrary functions (C library, especially)Special files and devicesFile formatsMiscellaneous stuffSystem administrationTry by typing man topic for something you're interested inIf it lists topics in different sections, specify the section

For more information:> man 3 sqrt> man manQuizUpcomingNext timePreprocessor directives#define#includeLanguage featuressizeofconstPrecedence and system limitsLab 2RemindersRead LPI chapter 11Class is canceled on Friday, March 11Lab for that week will be held on WednesdayThe company EC Key is sponsoring a contest to come up with novel uses for their BlueTooth door access technologyInterested? Come to the meeting this Friday, 1/22 at 3:30pm in Hoover 110Teams will be formed from CS, engineering, and business studentsAsk me for more information!Also, there's a field trip to Cargas Systems in Lancaster next Friday