<<

Programming

Programming Language • Is telling the computer how to do something • Definition: Applies specific programming languages to solve specific computational problems with solutions

Bits, Types, and Operations High-level • A programming language with strong abstraction from the details of the computer „ Isolates the execution semantics of a comparedtd to assembl bl/ly/low-lllevel • Advantage: Makes the process of developing a program simpler Based on slides © McGraw-Hill and more understandable Additional material © 2004/2005 Lewis/Martin Edited by Diana Palsetia

What does the Computer Understand? What does the Computer process & store?

At the lowest level, a computer has electronic “plumbing” An electronic device can represent uniquely only one „ Operates by controlling the flow of electrons through very fast of two things tiny electronic devices called „ Each “0” and Each “1” is referred to as a Binary Digit or „ Fundament unit of storage The devices react to presence or absence of voltage To represent more things we need more „ Could react actual voltages but designing electronics then becomes complex „ E.g. 2 bits can represent four unique things: 00, 01, 10, 11 „ k bits can distinguish 2k distinct items Symbolically we represent Combination binary bits together can represent some 1. Presence of voltage as “1” information or data. E.g. 01000001 can be 2. Absence of voltage as “0” 1. value 65 2. Alphabet (or ) ‘A’ in ASCII notation 3. Command to be performed e.g. Performing Add operation

CIT 593 3 CIT 593 4

1 Computer is a binary digital system Data

Binary (base two) system: What kinds of data do we need to represent? „ Has two states: 0 and 1 „ Numbers – signed, unsigned, integers, real, floating point, complex, rational, irrational, … „ Text – characters, strings, … Digital system: „ Logical –true, false „ Finite number of symbols or bits „ Images – pixels, colors, shapes, … „ If we want represent 3 or more values then we require multiple bits : „ “A particular representation is data type if there are operations in the computer that can operate on the information that is encoded All are characterized by number of bits the can in the representation.” (by Patt & Patel) store and process „ Most programming languages provide basic data types „ Example: 4, 8, 16, 32… „ Currently at 64-bit We’ll start with numbers…

CIT 593 5 CIT 593 6

Unsigned Integers Unsigned Integers (cont.)

Non-positional notation An n-bit unsigned integer represents 2n values „ Could represent a number (“5”) with a string of ones (“11111”) „ From 0 to 2n -1 „ Problems? 22 21 20 val Weighted positional notation 0 0 0 0 „ Like decimal numbers: “329” 0 0 1 1 „ “3” is worth 300, because of its position, while “9” is only worth 9 0 1 0 2 0 1 1 3 base-10 most least 1 0 0 4 significant significant (decimal) 329 101 base-2 1 0 1 5 (binary) 102 101 100 22 21 20 1 1 0 6 1 1 1 7 3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5

CIT 593 7 CIT 593 8

2 Base-2 Addition on Unsigned Data Signed Integers

Positive integers „ Just like unsigned with zero in most significant bit 00101 = 5

Negative integers „ Sign-magnitude: high-order bit to show negative, other bits are the same as unsigned 10101 = -5

„ One’s complement: flip every bit to represent negative 11010 =- 5

„ In either case, MS bit indicates sign: 0=positive, 1=negative

„ Both sign-magnitude and 1’s complement have problem

CIT 593 9 CIT 593 10

Problem Two’s Complement Sign-magnitude problem Idea „ Two representations of zero (+0 and –0) „ Find representation to make arithmetic simple and consistent

1’s complement problem Specifics „ Addition is complex „ For each positive number (X), assign value to its negative (-X), ¾Add two sign-magnitude numbers? such that X + (-X) = 0 with “normal” addition, ignoring carry out – e.g., try -12 + (13) 10011 (-12) +01101 (13) 00101 (5) 01001 (9) 1 00000 (0) +11011 (-5) + 10111(-9) 00000 (0) 00000 (0) ¾Need to add add back the carry into least significant bit (LSB) ¾Which will result in the correct result, -12+13 = 1

CIT 593 11 CIT 593 12

3 Two’s Complement (cont.) Two’s Complement Signed Integers

If number is positive or zero Range of an n-bit number: -2n-1 through 2n-1 –1 „ Normal binary representation, zeroes in upper bit(s) „ Note: most negative number (-2n-1) has no positive counterpart

If number is negative 3 2 1 0 3 2 1 0 „ Start with positive number 2 2 2 2 2 2 2 2 „ Flip every bit (i.e., take the one’s complement) 0 0 0 0 0 1 0 0 0 -8 „ Then add one 0 0 0 1 1 1 0 0 1 -7 0 0 1 0 2 1 0 1 0 -6 00101 (5) 01001 (9) 0 0 1 1 3 1 0 1 1 -5 (1’ s comp) (1’ s comp) 11010 10110 0 1 0 0 4 1 1 0 0 -4 + 1 + 1 0 1 0 1 5 1 1 0 1 -3 (-9) 11011 (-5) 10111 0 1 1 0 6 1 1 1 0 -2 0 1 1 1 7 1 1 1 1 -1

CIT 593 13 CIT 593 14

Converting Binary (2’s ) to Decimal More Examples

1. If leading bit is one, take two’s complement to get a positive number X = 00100111two 2. Add powers of 2 that have “1” in the n 2n =25+22+21+20 = 32+4+2+1 n 2n corresponding bit positions 0 1 =39 0 1 1 2 ten 1 2 3. If original number was negative, 2 4 2 4 add a minus sign 3 8 X = 11100110 3 8 4 16 two 4 16 5 32 -X=00011010 5 32 6 64 4 3 1 6 64 X = 01101000two =2 +2 +2 = 16+8+2 6 5 3 7 128 7 128 =2 +2 +2 = 64+32+8 8 256 =26ten 8 256 =104ten 9 512 X=-26 9 512 10 1024 ten 10 1024

Assuming 8-bit 2’s complement numbers. Assuming 8-bit 2’s complement numbers.

CIT 593 15 CIT 593 16

4 Converting Decimal to Binary (2’s C) Converting Decimal to Binary (2’s C) n 2n First Method: Division Second Method: Subtract Powers of Two 0 1 1. Change to positive decimal number 1. Change to positive decimal number 1 2 2. Divide by two – remainder is least significant bit 2. Subtract largest 2 4 3. Keep dividing by two until answer is zero , 3 8 recording remainders from right to left less than or equal to number 4 16 4. Append a zero as the MS bit; 3. Put a one in the corresponding bit position 5 32 if original number negative, take two’s complement 6 64 4. Keep subtracting until result is zero 7 128 5. Append a zero as MS bit; 8 256 X = 104 104/2 = 52 r0 bit 0 ten if original was negative, take two’s complement 9 512 52/2 =26 r0bit 1 10 1024 26/2 =13 r0 bit 2 13/2 = 6 r1 bit 3 X = 104ten 104 - 64 = 40 bit 6 6/2 = 3 r0 bit 4 40 - 32 = 8 bit 5 3/2 = 1 r1 bit 5 8 - 8 = 0 bit 3 1/2 = 0 r1 bit 6 X=01101000two X=01101000two

CIT 593 17 CIT 593 18

Operations: Arithmetic and Logical Addition

Recall 2’s comp. addition is just binary addition „ A data type includes representation and operations „ Assume all integers have the same number of bits Operations for signed integers „ Ignore carry out „ Addition „ For now, assume that sum fits in n-bit 2’s comp. representation „ Subtraction ¾ Assuming 8-bit 2’s complement numbers „ Sign Extension Logical operations are also useful (104) (-10) „ AND 01101000 11110110 „ OR +11110000(-16) + 11110111 (-9) „ NOT 1 01011000(88)1 1 1 1 0 1 1 0 1 (-19) And. . . carry(discard) „ Overflow conditions for addition

CIT 593 19 CIT 593 20

5 Subtraction Sign Extension

Negate 2nd operand and add To get correct results „ Assume all integers have the same number of bits „ Must represent numbers with same number of bits „ Ignore carry out „ For now, assume th at diff erence fit s i n n-bit 2’ s comp. What if we just pad with zeroes on the left? representation 4-bit 8-bit 01101000(104) 11110110 (-10) 0100 (4) 00000100 (still 4) -00010000(16) - 11110111(-9) 1100 (-4) 00001100 (12, not -4) 01101000(104) 11110110(-10) Now, let’s replicate the MSB (the sign bit) (-16) (9) +11110000 + 00001001 4-bit 8-bit 01011000 (88)1111111 1 (-1) 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4) Assuming 8-bit 2’s complement numbers.

CIT 593 21 CIT 593 22

Logical Operations Examples of Logical Operations Operations on logical TRUE or FALSE „ Two states: TRUE=1, FALSE=0 AND 11000101 „ Useful for clearing bits AND 00001111 A B A AND B A B A OR B A NOT A ¾AND witteo0h zero = 0 00000101 ¾AND with one = no change 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 OR 11000101 1 0 0 1 0 1 „ Useful for setting bits OR 1 1 1 1 1 1 00001111 ¾OR with zero = no change 11001111 ¾OR with one = 1

View n-bit number as a collection of n logical values NOT NOT 11000101 „ Operation applied to each bit independently (bitwise operators) „ Unary operation -- one argument 00111010 ¾Lot of use in water marking, cryptography, and new „ Flips every bit application that requires

CIT 593 23 CIT 593 24

6 Overflow Number System Overflow is said to occur if result is too large to fit in the People like to use decimal numbers number of bits used in the representation. „ Sum cannot be represented as n-bit 2’s comp number Computers use binary numbers „ E.ggg. Programming ggg Languages (8) (-8) 01000 11000 ¾ Translates decimal numbers into binary +01001 (9) + 10111 (-9) ¾The computer does all its arithmetic in binary 10001 (-15) 01111 (+15) ¾The languages translates binary results back into decimal

We have overflow if You occasionally have to use numbers in other number „ Signs of both numbers are the same, and Sign of sum is different systems „ If Positive number is subtracted from a Negative number, result is „ In Java, you can write numbers as octal , decimal , or positive and vice versa but not binary Any application involving numeric calculations should take of „ In HTML, colors are usually specified in hexadecimal notation: overflow situation otherwise consequences can be dire #FF0000, #669966, „ E.g. Control software malfunction of Ariane 5 Flight 501 space craft „ In C, you can write all notations

CIT 593 25 CIT 593 26

Hexadecimal Notation Hexadecimal Conversions

It is often convenient to write binary (base-2) numbers Binary to Hex as hexadecimal (base-16) numbers instead „ Every group of four bits is a hex digit „ Fewer digits: four bits per hex digit „ Start grouping from right-hand side „ Less error prone: easy t o corrupt tl long st tiring of f1’ 1’s and d0’ 0’s 0011 1010 1000 1111 0100 1101 0111 Binary Hex Decimal Binary Hex Decimal 0000 0 0 1000 8 8 3 8 F 4 D 0001 1 1 1001 9 9 A 7 0010 2 2 1010 A 10 0011 3 3 1011 B 11 Hex to Decimal 0100 4 4 1100 C 12 0101 5 5 1101 D 13 2 1 0 1AC16 = 1 x 16 + 10 x 16 + 12 x 16 = 42810 0110 6 6 1110 E 14 0111 7 7 1111 F 15 This is not a new machine representation, just a convenient way to write the number. CIT 593 27 CIT 593 28

7 Octal Numbers Octal to Binary Conversion

„ 3 digits per every octal group „ One octal digit equals three binary digits Binary Octal Decimal 101101011100101000001011 000 0 0 001 1 1 5 5 3 4 5 0 1 3 010 2 2 011 3 3 100 4 4 Octal to Decimal 101 5 5 110 6 6 2 1 0 111 7 7 1738 = 1x81 x 8 +7x8+ 7 x 8 +3x8+ 3 x 8 = 12310

CIT 593 29 CIT 593 30

Fractions: Fixed-Point Very Large and Very Small: Floating-Point

How can we represent fractions? Problem „ Use a “binary point” to separate positive from negative powers of „ Large values: 6.023 x 1023 -- requires 79 bits two (just like “decimal point”) „ Small values: 6.626 x 10-34 -- requires >110 bits „ 22s’s comp addition and subtraction still work ¾If binary points are aligned Use equivalent of “scientific notation”: F x 2E

2-1 = 0.5 Need to represent F (fraction), E (exponent), and sign 2-2 = 0.25 IEEE 754 Floating-Point (32-bits): 2-3 = 0.125 1b 8b 23b (()40.625) 00101000.101 S EtExponent FtiFraction +11111110.110(-1.25) 00100111.011(39.375) N = (−1)S ×1.fraction×2exponent−127 ,1≤ exponent≤ 254 S −126 No new operations -- same as integer arithmetic N = (−1 )×0.fraction×2 , exponent= 0

CIT 593 31 CIT 593 32

8 Floating Point Example Floating Point (contd..) N = −1S ×1.fraction × 2exponent −127 ,1 ≤ exponent ≤ 254 - > Normalized Single-precision IEEE floating point number N = −1S × 0.fraction × 2−126 , exponent = 0 - > Denormalized 1 01111110 10000000000000000000000 Zero sign exponent ftifraction „ Exponent field & fraction field is all 0’s „ -0 and +0 are distinct values, though they both compare as „ Sign is 1: number is negative equal „ Exponent field is 01111110 = 126 (decimal) Infinity (positive & negative) -1 „ Fraction is 100000000000… = 2 = 1/2 = 0.5 (decimal) „ Exponent all 1’s and Fraction all 0’s NaN (Not a Number) Value = -15x21.5 x 2(126-127) = -15x21.5 x 2-1 = -0750.75 „ When does this occur? „ An invalid operation is also not the same as an „ Arithmetic overflow (which might return an infinity) „ An arithmetic underflow (which would return the smallest normal number, a , or zero)

CIT 593 33 CIT 593 34

Fun Experiment

Try this experiment: Open up a new file in Notepad and type – “Four score and seven years ago”

You find the file to be 30 in size „ Notepad stores the sentence in a file on disk ¾File is nothing but sequence of characters ¾The file will also contain 1 per character (note that space is also a character)

CIT 593 35 CIT 593 36

9 Storage Space for Numerics Other Data Types

Numeric types in Java are characterized by their size: how much Text strings memory they occupy „ Sequence of characters, terminated with NULL (0) „ 1 byte = 8 bits size range „ Typically, no hardware support Integral types byte 1 byte -128: 127 short 2 bytes -32768:32767 Image char 2 bytes 0:65535 „ Array of pixels int 4 bytes -2147483648:2147483647 „ Monochrome: one bit (0/1 = black/white) long 8 bytes … „ Color: red, green, blue (RGB) components (e.g., 8 bits each) „ Typically no hardware support Floating point types size largest smallest > 0

float 4 byte 3.4E38 1.4E-45

double 8 bytes 1.7E308 4.9E-324 Sound „ Sequence of fixed-point numbers What about C Language?

„ Are platform dependent CIT 593 37 CIT 593 38

10