External vs Internal Representation of Data External ● Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ● Sign/Magnitude Notation: + or -, immediately to the next of digits ● Decimal Notation: Digits with a decimal point ● Letters and Symbols: A -> Z, a -> z, @, $, %, &, *, etc ● Colors: Blue, Green, Orange, Purple, Red, etc ● Audio Internal - Binary ● 0 -> Logical False / Off ● 1 -> Logical True / On ● A Binary number stored in the memory of a computer takes on meaning only because it is used in a certain way Vocabulary Decimal (dec.): These are decimal systems that use zero to nine. The prefix dec- means ten Digit: Each number in the Decimal Number System, zero to nine Binary (bin. ): base-two numeral system that uses zero and one. The smallest unit of information a computer can process Byte: Eight bits. Each cell or address in Main Memory contains one byte of data Octal (oct. ): Base-eight numeral system that uses zero to seven Hexadecimal (hex. ): Base 16 numeral system that uses zero to nine and A to F Nybble: Half of one byte or four bits Storage ● The number of different values that can be stored using n bits is 2 n ● Because a byte is an exceedingly small number of memory, we use larger units ● When going from x bit sequence to y bit sequence, memory is changed 2 x-y times ● Eg: When upgrading from 8 bit to 12 bit, you get 2 12-8 = 2 4 = 16 times more memory Units Actual # of Bytes (Exponent) Actual # of Bytes (Decimal) Approximate # of Bytes Bytes 2 0 1 One KiloBytes (KB) 2 10 1024 One Thousand MegaBytes (MB) 2 20 1,048,576 One Million GigaBytes 2 30 1,073,741,824 One Billion TeraBytes 2 40 1,099,511,627,776 One Trillion Converting Numbers Example(s) 1. Express the Base-10 number 23,745 as a sum of the Base-10 numeral system 2 3 , 7 4 5 10 4 10 3 10 2 10 1 10 0 2(10,000) + 3(1000) + 7(100) + 4(10) +5(1) 20000 + 3000 + 700 + 40 + 5 Binary to Decimal Example(s) 1. Convert 101101 BIN to a Decimal number 1 0 1 1 0 1 2 5 2 4 2 3 2 2 2 1 2 0 32 16 8 4 2 1 32 + 8 + 4 + 1 45 2. Convert 1110 BIN to a Decimal number 1 1 1 0 2 3 2 2 2 1 2 0 8 4 2 1 8 + 4 + 2 14 Converting Binary to Decimal Example(s) 1. Convert 102 DEC as a Binary number Decimal Base Quotient Remainder 102 2 51 0 51 2 25 1 25 2 12 1 12 2 6 0 6 2 3 0 3 2 1 1 1 2 0 1 Read the remainders from the bottom to top 1100110 2. Convert 78 DEC as a Binary number Decimal Base Quotient Remainder 78 2 39 0 39 2 19 1 19 2 9 1 9 2 4 1 4 2 2 0 2 2 1 0 1 2 0 1 1001110 Overflow: Attempts to represent a value that exceeds the maximum allowed values Java Integers are 4 Bytes, allowing integers in the range -2,147,483,648 to 2,147,483,647. Because most computed numbers are within this range and making it infinite would be impractical for most users Examples: 1. Convert 102 DEC to a 4-Bit Binary Number 1100110 BIN, 7 bits Overflow Maximum possible values = 2 4 = 16 possible values (0 - 15) 2. Convert 102 DEC to a 12-Bit Binary Digits 1100110 BIN, 12 - 7 = 5 Add 5 extra bits (only zeroes) in the front of the number so that the original value doesn’t change 000001100110 0110101 Hexadecimal Numbers: ● Hexadecimal (Base-16) is a common number system in Computer Science. Since there are only 10 digits (0 - 9, the first 6 letters (A - F) are used to represent the remaining six characters ● Each character in HEX represents four Bits (nybble). To represent a full byte, two hexadecimal characters are used. These range from 00 (0) to FF (255) ● Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Hexadecimal Decimal Binary 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Hexadecimal to Binary: ● To convert hexadecimal to binary, look at each nybble individually Example(s) 1. Convert D3B HEX to a Binary Number D 3 B 13 3 11 110100111011 2. Convert 40F HEX to a Binary Number 4 0 F 4 0 15 010000001111 Convert hexadecimal to decimal: Example(s): 1. Convert 40F HEX to a Decimal number 4 0 F 4 0 15 16 2 16 1 16 0 4(256) + 0(16) + 15(1) 1039 Convert Binary to Hexadecimal: Example(s): 1. Convert 100111000001 BIN to a Hexadecimal Number 1001 1100 0001 9 12 1 9 C 1 9C1 HEX 2. Convert 011000111110 BIN to a Hexadecimal Number 0110 0011 1110 6 3 14 6 3 E 63E HEX Converting Decimal to Hexadecimal ● To convert Decimal to Hexadecimal, we will follow the same process as converting Decimal to Binary, except we will set our base 16 rather than 2 1. Convert 5236 DEC to a Hexadecimal Number Decimal Base Quotient Remainder 5236 16 327 4 327 16 20 7 20 16 1 4 1 16 0 1 1474 HEX Convert Octal to Decimal Base 8 or 2 3 To convert to binary, each octal digit becomes 3 binary digits Example(s): 1. Convert 174 OCT to a Decimal Number 1 7 4 001 111 100 4(8 0 ) + 7(8 1 ) + 1(8 2 ) 4 + 56 + 64 124 DEX Convert from Decimal to Octal ● To convert Decimal to Octal, follow the same process as converting Decimal to Binary or Decimal to Hexadecimal. Except, use 8 as your base ● Students should be able to convert numbers from any base to another base Adding Binary Numbers Example(s): 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 (write 0 and carry over 1) 1. Find the sum of 111 BIN and 1110 BIN 1 1 1 0 0 1 1 1 0 1 1 1 0 1 0 1 0 1 2. Find the sum of 0110101 BIN and 0011101 BIN 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 1 0 Sign/Magnitude Notation ● Sign/Notation allows the representation of both positive and negative numbers ● Sign/Magnitude Notation requires one additional bit where ● Binary 0 -> Positive ● Binary 1 -> Negative ● The leftmost bit is used to represent the sign Example(s) 1. Convert 5 DEC to a Binary number using Sign/Magnitude Notation 101 BIN -> 0101 BIN 2. Convert -5 DEC to an 8-Bit Binary Number using Sign/Magnitude Notation 101 BIN -> 10000101 BIN Two’s Complement Representation ● Two’s Complement Representation is one of the most used signed integer representation 3-Bit Two’s Complement Representation Binary Integer 000 0 001 1 010 2 011 3 100 -4 101 -3 110 -2 111 -1 If the int is positive, it is the same as the binary representation If the int is negative, convert the positive to binary, invert all 0s and 1s, add 1 to the new number Unlike sign/magnitude notation, arithmetic works in Two’s Complement Representation Example(s) 010(1) + 110(-2) = 000 Fractional Numbers ● Fractional Numbers can be represented in Binary by converting the number to Scientific Notation and using a Signed-Integer Technique ● We will use the following criteria when representing fractional numbers in Binary - 16 Bits - Sign/Magnitude Notation - Scientific Notation: ±M x B E ● When the binary is stored in a computer, there are no decimals ● -1 as a fractional number is 1100000000000000 1-Bit 9-Bits 1-Bit 5-Bits Sign of M Mantissa Sign of E Exponent 1 100000000 0 00000 Negative Binary-1 Positive Binary 0 Example(s) 1. 5.75 5 7 5 10 0 10 -1 10 -2 5 + 7(1/10) + 5(1/100) = 5 75/100 = 5.75 2. 11.101 1 1 1 0 1 2 1 2 0 2 -1 2 -2 2 -3 2 + 1 + ½ + 0(¼) + ⅛ = 3.625 Example(s) 1. Convert 5.75 to a binary number 5.75 5 DEC = 101 BIN 0.75 x 2 = 1.5 0.5 x 2 = 1 0.75 DEC = 0.11 BIN 5 DEC + 0.75 DEC = 101 BIN + 0.11 BIN = 101.11 BIN 101.11 to scientific notation 3 places before decimal point 3 DEC = 11 BIN Scientific notation = 0.10111 * 2 11 (11 in Binary, not 11 in Decimal) Sign of Mantissa Mantissa Sign of Exponent Exponent 0 (positive) 101110000 0 (positive) 00011 In mantissa, filler zeroes go at the end In exponent, filler zeroes go at the front Final answer: 0101110000000011 2. Convert -17.625 to a Binary Number 17 DEC = 10001 0.625 x 2 = 1.25 0.25 x 2 = 0.5 0.5 x 2 = 1 0.625 DEC = 0.101 BIN 17.625 DEC = 10001.101 BIN 5 places before decimal Scientific notation: 0.10001101 x 2 101 Sign of Mantissa Mantissa Sign of Exponent Exponent 1 (negative) 100011010 0 (positive) 00101 Final answer = 1100011010000101 3. Convert 1110001110000101 to a decimal number Sign of Mantissa Mantissa Sign of Exponent Exponent 1 (negative) 110001110 0 (positive) 00101 Exponent = 101 BIN = 5 DEC 0.110001110 x 2 5 11000.1110 11000 = 24 0.111 = (1(1) + 1(2) + 1(4))/2 3 = ⅞ = 0.875 Final answer: -24.875 Round-Off Errors: Occurs because of the way numbers with decimal points are stored in the computer. This imprecise nature can cause rounding errors and possibly inaccurate results in your program Textual Information ● Computers can only understand numbers, so letters and symbols must be converted to numbers Code Mapping: Assigns each printable letter, symbol, emoji in our system a unique number ● ASCII (American Standard Code for Information Interchange is an 8 Bit mapping. One is a parity bit for error checking. Thus, ASCII is identified as a 7-Bit mapping ● Unicode is a 16-Bit mapping with a 32-Bit variant ● For example, the ASCII representation of Dog is 01000100 01101111 01100111 Example(s) 1. How much storage is required for a Novel containing 100,000 words using an ASCII (8-Bit) Character Mapping? Assume the average word length is 5 and ignore spaces and special characters Total bits = 100,000 x 5 x 8 = 4,000,000 Bits 4,000,000 bits = 500,000 bytes 1 Kilobyte = 1024 Bytes 500,000 Bytes = 500,000/1024 = 488.28 KB Final answers must be rounded to 2 Decimal places Do not round off until reaching the final answer Sound ● Sound is an analog sound ● Sound is digitized using a technique called sampling Sampling: At fixed time intervals, the amplitude of the signal is measured and stored as an integer value ● Accuracy of original sound depends on two key parameters: - Sampling Rate: Times per second the amplitude was sampled - Bit Depth: Number of Bits used to encode each sample Example(s) 1. How much storage is required for a one-minute MP3 file using a Bit Depth of 16 Bits per Second? MP3 uses 44,100 samples per second Total bits = 44,100 * 16 * 60 = 42, 336, 000 bits 42, 336, 000 Bits/8 = 5,292,000 Bytes 5/292,000/1024 = 5167.96875 KB 5167.96875 Kilobytes / 1024 = 5.05 MB Color ● The human eye primarily detects red, green, and blue. Other colors are a combination of these three colors in different amounts ● The early color representation used a Byte (8-Bits) for using three bits of red, three for green, and two for blue. RRRGGGBB . Only 128 Colors could be represented using 1 Bit ● Today, computers use 1 Byte for each color RRRRRRRRGGGGGGGGBBBBBBBB ● We can create - 256 shades of Red - 256 shades of Green - 256 shades of Blue - 16, 777,216 total colors ● The binary representation of these colors would be very long, so this is an example where hexadecimal equivalents are commonly used Pixel: Short for picture element, the pixel is the basic unit of color on a computer display ● The size of pixels on a screen varies depending on the display’s resolution ● Each pixel is represented as binary numbers ● Large pixels can make an image look blocky, a phenomenon known as pixelation Boolean Logic: Area of mathematics that deals with rules for manipulating the two logical values, true or false ● Boolean expressions were introduced in the mid-1800s by George Boole ● Boolean logic has become prevalent in digital electronics and programming ● Boolean Logic is used to construct circuits that perform operations - true - Binary 1 - false - Binary 0 ● There are three basic operations in Boolean Logic AND Conjunction & && • OR Disjunction | || + NOT Negation ~ ! ¬ ● We will also use a fourth operator, exclusive or , or symbolized as XOR AND - Binary Operator (Requires two operands) ● When an expression uses an AND operator, then both sides of the expression must be true for the entire expression to evaluate to true ● The expression a AND b is true only if both a and b are true Example(s): To be the President of the United States, one must be at least 35 years old AND be a natural-born citizen OR - Binary Operator (Requires two operands) ● When the OR operation is used between two expressions, then only one thing has to be true for the entire expression to evaluate to true ● If both things are true then the expression satisfies the requirement for at least one thing be true, so it evaluates to true ● The only way an expression containing OR operator can evaluate to false is if all included statements are false ● In the expression a OR b, if a is true, the entire expression is true regardless of what b is. It is also true if b is also true, regardless of a is true or false Example(s): The requirements to see an R-rated movie are that you must be 17 years old OR be accompanied by someone who is 21 years old or older NOT - Unary Operator (Requires one operand) ● The NOT operation simply reverses the associated expression ● If the expression evaluates to true, it becomes false, and if it is false, it becomes true XOR - Binary Operand (Requires two operands) ● An XOR operator is made up of AND & OR gates ● If two expressions are separated by an XOR operator, then exactly only one of the two things has to be true. If neither is true or both are true, the whole expression is false Example(s): Taking the bus (X)OR driving to school Truth/Logic Tables Truth/logic tables: Shows possible values for inputs on a true or false, value for the overall expression’s truth a b a AND b false false false false false false true true false true true true a b a OR b false false false false false true true true true true true true a NOT a false true true false a b a XOR b false false false false true true true false true true true false Relational operators > Greater than >= Greater than or < Less than <= Lesser than or Equal to == Equal != Not Equal To Example(s): Let x = 2, y = 5 and z = 8. Determine the value of the expression 1. (x == 2) AND (y == 7) 2 == 2 AND 5 == 7 TRUE AND FALSE FALSE or 0 2. (x < 4) AND (y >= 5) AND (z == 8) (2 < 4) AND (5 >= 5) AND (8 == 8) TRUE AND TRUE AND TRUE TRUE AND TRUE TRUE or 1 3. (x == 2) AND (y == 6) OR NOT ((x + y) < z) (2 == 2) AND (5 == 6) OR NOT ((2 + 5) < 8) TRUE AND FALSE OR NOT TRUE FALSE OR NOT TRUE FALSE OR FALSE FALSE or Binary 0 Writing Boolean Expressions Interpretation of common phases a is # a = # a is at most # a <= # a is at least # a >= # a is between # and # a >= # AND a <= # Example(s) 1. Write a Boolean Expression that is true when the value of x is between 0 and 50 inclusive. x >= 0 AND x <= 50 2. Write a Boolean expression that is true if the value of x is 2 or atleast 12 x == 2 OR x >= 12 If asked to write a expression that is false, write an expression that is true, then use the NOT operator Ex: NOT(x == 0 AND x == 50) Example(s) 1. Create a truth table for (NOT a AND b) or c a b c (NOT a AND b) OR c 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 Applying NOT Operator NOT(x >= 0 AND X == 50) x < 0 OR X != 50 DeMorgan’s Law NOT(a OR b) = NOT a AND NOT b NOT(a AND b) = NOT a OR NOT b Simplifying Boolean Expressions with Relational Operator NOT(a >= b) a < b NOT(a > b) a <= b NOT(a <= b) a > b NOT(a < b) a >= b Example(s) 1. Write a Boolean expression that is false if the value of x is not 5 and the value of y is at most 30 NOT(x != 5 AND y <= 30) x == 5 OR y > 30 Short Circuiting; An expression is stopped being evaluated as soon as the outcome is determined For AND statement, it stops evaluating when there is a false FOR OR statement, it stops evaluating when there is a true Logic Gates Logic gates: A physical device that can carry and read out logical operations by taking one or more Boolean values as input and producing one Boolean value as output ● When talking about computers and electronic devices, Boolean values are 0 and 1 - 0 - No current is running through the wire - 1 - Current running through the wire Circuit: A collection of logic gates that transforms a set of binary inputs into a set of binary outputs AND OR NOT XOR Example(s) Create a circuit for the given Boolean expression 1. a AND b AND c OR NOT d 2. NOT a OR b AND NOT c