CISC181 010-012 09/14/05 05F Attendance: (1) Calendar is online. Review from last time: (2) Conversion from decimal to binary by hand 271 in decimal is what in binary? 271 - 256 = 15 15 - 8 = 7 7 - 4 = 3 3 - 2 = 1 1 - 1 = 0 So the bits are 256, 8, 4, 2, 1 as shown here: 512 256 128 64 32 16 8 4 2 1 0 1 0 0 0 0 1 1 1 1 New material: (3) Meaning of % and / with integers in C, C++, Java When you see the / in C, C++ or Java, beware! Alarm bells should go off. You need to check the numerator and denominator. If both are of type "int" (or any integer type), then you are going to get "integer division" instead of regular division. Integer division throws away the remainder. Integer division does NOT ROUND !!!!!! The remainder after integer division can be retreived with the % function. The % function is not "percent", but rather is the "modulus" function, or just "mod" for short. So, you'll never (hopefully) hear me call it the percent sign again... we will read it as "mod". 271 % 2 is read "two seventy one mod two". The meaning of % is illustrated by "3rd grade math class style division": 2 "guzinta" 271 135 times, "remainder 1" we write 135 r 1 The "r 1" part is the result of 271 % 2. 135 r 1 +----------- 2 | 271 2 = 07 6 = 11 10 == 1 Some practice: 25 % 5 = 0 27 % 5 = 2 37 % 6 = 1 x % 57 = ? The smallest value that could be the answer is: 0 The largest value that could be the answer is: 56 % is remainder after division. (4) A technique to convert from decimal to binary using % and / 271 % 2 => 1 271 / 2 => 135 135 % 2 => 1 135 / 2 => 67 67 % 2 => 1 67 / 2 => 33 33 % 2 => 1 33 / 2 => 16 16 % 2 => 0 16 / 2 => 8 8 % 2 => 0 8 / 2 => 4 4 % 2 => 0 4 / 2 => 2 2 % 2 => 0 2 / 2 => 1 1 % 2 => 1 1 / 2 => 0 What we get from the results of the % operations is the bits of the number we started with, in reverse order. (From least significant to most significant). 512 256 128 64 32 16 8 4 2 1 0 1 0 0 0 0 1 1 1 1 (5) Review from last time about character data... and a bit more information... "ADAMS" in memory can be represented with (at least) two types of strings: (1) C-Strings.... (2) C++ "string" class. You may have also encountered "AP Strings" if you go "way back".... C++ strings are much more convenient, but they hide a lot of detail. When learning about computers, you may learn more if you work with C-style strings first. You'll learn better habits of thinking about how computer memory works. Also, for interacting with the operating system, with network programming (e.g. for web clients and web servers), you need to know about C-strings... you can't avoid them. So we will learn about C-strings first, and learn about C++ style strings later. C-strings are just arrays of characters, terminated with the '\0'. The '\0' is different from the '0' character. The '0' character prints the actual symbol for zero (a kind of skinny tall oval.) The '\0' is never printed... instead, it is a kind of internal stop sign to tell the computer when to stop processing a string. It is, the words of the Gov. of Calif. the "null terminator". ADAMS in memory is represented by 6, not 5, consecutive byte. How many bits in a byte? eight bits in a byte. What's half a byte? A nibble (four bits). So altogeher, in ADAMS, there are 48 bits. characters A D A M S \0 ascii values 65 68 65 77 83 0 (in decimal) in binary: 0100 0001 0100 0100 0100 0001 0100 1101 0101 0011 0000 0000 [We can demonstrate that these are the values, and check our work, at least for the five capital letters, using emacs and a utility called "od" Unfortunatly, it appears od only gives us octal, decimal, or hexadecimal, so we'll have to come back to it later...] (6) Going back to the algorithm for converting decimal to binary with repeated % and / ... This raises a question: Can we make this a function? Use a C string as our target: char binaryAnswer[9]; Limitations: min 0 minimum value for decimal input max 255 maximum value for decimal input number of bits in binary answer 8 represent the binary answer as a C string char binaryAnswer[9]; Note the role of \0 '0' in binary is 0010 1000 '1' in binary is 0010 1001 '\0' in binar is 0000 0000 [null terminator] void computeBinary (int decVal, char binAns[9]) { for (int i = 0; i<8, i++) binAns[i] = '0'; binAns[8] = '\0'; @@@ FILL IN DETAILS HERE return; }