=============================================================== Information Representation: binary numbers =============================================================== - The bit as a unit of information - A digital computer stores information (numbers, letters, images, sound, video, etc.) as numbers, specifically binary numbers. - A binary number (base-2 number) is made up of 1's and 0's. - Voltage level on a wire is interpreted as 0 or 1: 0 - voltage level close to 0 volts. 1 - voltage level greater than a certain level, e.g., 3 volts. . Each wire can represent a 'bit' of information. . If you have 8 such wires, you can represent 8 bits of data. . If you have 1 bit, how many variations can you represent? 2 bits, 3 bits, 4 bits, ... k bits, - If you want to represent 17 different things/variations by assigning a unique bit pattern to each of the 17 different things/variations, how many bits would be required? - How many different keys would you see on a typical computer keyboard? Count shift keys separately. And how many bits would you need to be able to uniquely represent all of the keys on a keyboard? - ASCII code to represent keys on a keyboard . ASCII : American Standard Code for Information Interchange . Almost all computer manufacturers have agreed to use this code for transferring character codes between the main computer processing unit and the input/output devices. . How many distinct symbols do you see on a typical computer keyboard? (again) . ASCII code is an 8-bit code . How would you represent "Java - Fun!" using ASCII code? (Refer to ASCII.txt for a complete list of ASCII codes) Decimal Octal Hex 8-bit Binary Value ------- ----- --- ------------ ----- 074 112 04A 01001010 J 097 141 061 01100001 a 118 166 076 01110110 v 097 141 061 01100001 a 032 040 020 00100000 SP (Space) 045 055 02D 00101101 - (minus or dash) 032 040 020 00100000 SP (Space) 070 106 046 01000110 F 117 165 075 01110101 u 110 156 06E 01101110 n 033 041 021 00100001 ! (exclamation mark) Java - Fun! = 074 097 118 097 032 045 032 070 117 110 033 in Decimal (without the separating spaces) = 01001010 01100001 01110110 01100001 00100000 00101101 00100000 01000110 01110101 01101110 00100001 in binary (without the separating white spaces) = 0100101001100001011101100110000100100000001011010010000001000110011101010110111000100001 (eliminating blank spaces) . Given this bit string, how would you interpret it back to come up with "Java - Fun!"? . Java does not actually use ASCII code encoding, it uses the Unicode encoding scheme with 16 bits (actually more than 16 bits now, but 16 is not a bad number to use for our purpose.) - Meaning of bit (or digit) patterns . Let's consider the case where we want to interpret a bit pattern as a number, a binary number. . 00000101 (binary) for five cf. 345601 (decimal) = = 3 x 10^5 0 x 2^7 + 4 x 10^4 + 0 x 2^6 + 5 x 10^3 + 0 x 2^5 + 6 x 10^2 + 0 x 2^4 + 0 x 10^1 + 0 x 2^3 + 1 x 10^0 + 1 x 2^2 + 0 x 2^1 + 1 x 2^0 = 5 - What happens when you type in 325 as an integer input on your keyboard for your program that reads in an integer? - Binary to decimal conversion . 101010011 = ? - Decimal to binary conversion . Let's talk about how to do it (an algorithm) first. . By the way, there is an easy technique that we can use: 26 remainder 2 13 0 (divide 26 by 2: 13 with 0 as remainder) 2 6 1 (divide 13 by 2: 6 with 1 as remainder) 2 3 0 2 1 1 thus, 11010 = 26 (collect the last quotient and remainders bottom up) - Adding binary numbers 101010 + 10101 ----------- 101010 + 110101 ----------- 100101111 + 110101010 --------------- - Subtraction with binary numbers 10101010 - 1010100 ------------- - Primitive data types in Java (8 of them in all) Each data type has a fixed maximum size in number of bits it uses to represent values of that type. E.g., byte: 8 bits short: 16 bits (signed) int: 32 bits (signed) long: 64 bits (signed) float: 32 bits (signed) double: 64 bits (signed) char: 16 bits (Unicode) boolean: 1 bit (but Java uses 8 bits for convenience) This semester, we will be using int, double, char, and boolean. We will use long occasionally for really large numbers. You can sort of see what the range of numbers a variable of one of these types can hold in memory. What do you do if you want to compute with large numbers, larger than the numbers that can be represented by these types? For a signed data type, one bit (most significant bit) is used to indicate the sign (+ or -, usually 0 for positive 1 for negative) Wait a minute? Why do we use 32 bits to store a small number like 7? Why not use 3 bits (or 4 bits if we want to include a sign bit)? And, 4 bits for 12 (or 5 bits if we want to include a sign bit) 5 bits for 18 (or 6 bits if we want to include a sign bit) and so on ... Other more complex data types will be built by combining primitive data types in some fashion. For example, a string is an array of characters. For example the string "Awesome" consists of characters 'A', 'w', 'e', 's', 'o', 'm', and 'e' in that order in a data structure known as array. We will study arrays soon. How are floating point numbers represented? . One bit for the sign . Some number of bits for the decimal portion of the number . Some number of bits for the fractional portion of the number - Now let's see some operations on logical variables, i.e., the 'logical' operations A logical variable can have one of two values: 0 (false) or 1 (true) 'false' and 'true' are logical values (also called boolean values named after a logician named Boole) There are three logical operators that we will learn in Java: 1. NOT (represented as the symbol '!' in Java) 2. AND (represented as the symbol '&&' in Java) 3. OR (represented as the symbol '||' in Java) Example: If a, b, and c are boolean variables, then !a a && b a || b a || b && c (a || b) && c || a are all examples of logical expressions in Java. We will see what they mean afer we study the meaning of each of these operators now. - The NOT (!) function . NOT is a unary logical function . requires only one source operand . also known as the 'complement' operation . output is obtained by inverting the input . Truth table representation for NOT A (NOT A) --------------- 0 1 1 0 - The AND (&&) function . requires two source operands . output is 1 if both sources have the value of 1 0 otherwise . Truth table representation for AND A truth table consists of n + 1 columns and 2^n rows, where n is the number of operands: A B (A AND B) ----------------- 0 0 0 0 1 0 1 0 0 1 1 1 - The OR (||) function . requires two source operands . output is 1 if any source has the value of 1 0 otherwise . Truth table representation for OR A truth table consists of n + 1 columns and 2^n rows, where n is the number of operands: A B (A OR B) ----------------- 0 0 0 0 1 1 1 0 1 1 1 1 - Logical expressions: If a, b, and c are boolean variables, then !a a && b a || b a || b && c (a || b) && c || a are all examples of logical expressions in Java What are their values? We will do this together in class.