Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming with Java, for Beginners

Similar presentations


Presentation on theme: "Introduction to Programming with Java, for Beginners"— Presentation transcript:

1 Introduction to Programming with Java, for Beginners
Want to look at how computers work, but first, we’ll look at the representation of data. Bits, Data Types, and Operations Based on slides © McGraw-Hill Additional material © 2004/2005 Lewis/Martin

2 Computer Organization
Application Program Algorithms Language Software Hardware Computer organized as levels of transformation. A problem stated in natural language such as English is actually solved by electrons moving around inside the electronics of the computer. Instruction Set Architecture (and I/O Interfaces) Microarchitecture Circuits Devices

3 What does the Computer Understand?
At the lowest level, a computer has electronic “plumbing” Operates by controlling the flow of electrons through very fast tiny electronic devices called transistors The devices react to presence or absence of voltage Could react actual voltages but designing electronics then becomes complex Symbolically we represent 1. Presence of voltage as “1” 2. Absence of voltage as “0” Harder to measure the voltage through wall outlet. Easy to detect no voltage or voltage…

4 What does the Computer process & store?
An electronic device can represent uniquely only one of two things Each “0” and Each “1” is referred to as a Binary Digit or Bit Fundament unit of information storage To represent more things we need more bits E.g. 2 bits can represent four unique things: 00, 01, 10, 11 k bits can distinguish 2k distinct items Combination binary bits together can represent some information or data. E.g can be 1. Decimal value 41 2. Alphabet (or character) ‘A’ in ASCII notation 3. Command to be performed e.g. Performing Add operation

5 Data What kinds of data do we need to represent? Data type:
Numbers – signed, unsigned, integers, real, floating point, complex, rational, irrational, … Text – characters, strings, … Images – pixels, colors, shapes, … Logical – true, false ...... Data type: Representation and operations within the computer We’ll start with numbers…

6 101 329 Unsigned Integers 22 21 20 102 101 100 Non-positional notation
Could represent a number (“5”) with a string of ones (“11111”) Problems? Weighted positional notation Like decimal numbers: “329” “3” is worth 300, because of its position, while “9” is only worth 9 Non-positional: Try adding (or, more extremely, dividing) two numbers using this number representation system). Try adding (or, more extremely, dividing) two numbers using this number representation system). Question: What is an integer? Whole number (positive or negative number with no fractional part). Aside: ENIAC was both non-positional (for each decimal digit) and positional 101 22 21 20 1x4 + 0x2 + 1x1 = 5 most significant least base-10 (decimal) 329 102 101 100 base-2 (binary) 3x x10 + 9x1 = 329

7 Unsigned Integers (cont.)
An n-bit unsigned integer represents 2n values From 0 to 2n-1 7 1 6 5 4 3 2 val 20 21 22

8 Operation on Unsigned Data
Base-2 addition – just like base-10! Add from right to left, propagating carry carry 10111 + 111 (18) (18) (15) (9) (11) (1) Do example: in class 1 1 1 1 1 1 1 1 1 (27) (29) (16) (23) (7) 1 1 1 1 (30)

9 Signed Integers Positive integers Negative integers
Just like unsigned with zero in most significant bit = 5 Negative integers Sign-magnitude: set high-order bit to show negative, other bits are the same as unsigned = -5 One’s complement: flip every bit to represent negative = -5 In either case, MS bit indicates sign: 0=positive, 1=negative Both sign-magnitude and 1’s complement have problem Indus Valley Civilization (Pakistan) “discovered” negative number c 2600BC. (India “discovered” 0 about the same time.)

10 E.g. One’s Complement Problem
Addition is complex add two sign-magnitude numbers? e.g., try (13) Need to add add back the carry into least significant bit (LSB) Which will result in the correct result, = 1 10011 (-12) (13) (0)

11 Two’s Complement + 11011 (-5) + (-9) 00000 (0) 00000 (0) 1 1 1 1 Idea
Find representation to make arithmetic simple and consistent Specifics For each positive number (X), assign value to its negative (-X), such that X + (-X) = 0 with “normal” addition, ignoring carry out 00101 (5) (9) (-5) + (-9) 00000 (0) (0) 1 1 1 1

12 Two’s Complement (cont.)
If number is positive or zero Normal binary representation, zeroes in upper bit(s) If number is negative Start with positive number Flip every bit (i.e., take the one’s complement) Then add one Why does this work? 1’s comp A + !A > gives you a bunch of ones; add one more to turn them all to zeros! 00101 (5) (9) 11010 (1’s comp) (1’s comp) 11011 (-5) 1 1 1 1 (-9)

13 Two’s Complement Signed Integers
Range of an n-bit number: -2n-1 through 2n-1 – 1 Note: most negative number (-2n-1) has no positive counterpart 23 7 1 6 5 4 3 2 20 21 22 1 23 -1 -2 -3 -4 -5 -6 -7 -8 20 21 22

14 Converting Binary (2’s C) to Decimal
If leading bit is one, take two’s complement to get a positive number Add powers of 2 that have “1” in the corresponding bit positions If original number was negative, add a minus sign 1024 10 512 9 256 8 128 7 64 6 32 5 16 4 3 2 1 2n n X = two = = = 104ten Assuming 8-bit 2’s complement numbers.

15 More Examples X = 00100111two = 25+22+21+20 = 32+4+2+1 = 39ten
= = = 39ten 1024 10 512 9 256 8 128 7 64 6 32 5 16 4 3 2 1 2n n X = two -X = = = = 26ten X = -26ten Assuming 8-bit 2’s complement numbers.

16 Converting Decimal to Binary (2’s C)
First Method: Division Change to positive decimal number Divide by two – remainder is least significant bit Keep dividing by two until answer is zero, recording remainders from right to left Append a zero as the MS bit; if original number negative, take two’s complement What if we don’t append a 0 at end? X = 104ten 104/2 = 52 r0 bit 0 52/2 = 26 r0 bit 1 26/2 = 13 r0 bit 2 13/2 = 6 r1 bit 3 6/2 = 3 r0 bit 4 3/2 = 1 r1 bit 5 1/2 = 0 r1 bit 6 X = two

17 Converting Decimal to Binary (2’s C)
1024 10 512 9 256 8 128 7 64 6 32 5 16 4 3 2 1 2n n Second Method: Subtract Powers of Two Change to positive decimal number Subtract largest power of two less than or equal to number Put a one in the corresponding bit position Keep subtracting until result is zero Append a zero as MS bit; if original was negative, take two’s complement What if we tried to go right to left? X = 104ten = 40 bit 6 = 8 bit 5 8 - 8 = 0 bit 3 X = two

18 Sign Extension To get correct results
Must represent numbers with same number of bits What if we just pad with zeroes on the left? Now, let’s replicate the MSB (the sign bit) 4-bit 8-bit 0100 (4) (still 4) 1100 (-4) (12, not -4) 4-bit 8-bit 0100 (4) (still 4) 1100 (-4) (still -4)

19 Operations: Arithmetic and Logical
Recall A data type includes representation and operations Operations for signed integers Addition Subtraction Sign Extension Logical operations are also useful AND OR NOT And. . . Overflow conditions for addition

20 Addition + 11110000 (-16) + 11110111 (-9) 1 01011000 (88) (-19)
2’s comp. addition is just binary addition Assume all integers have the same number of bits Ignore carry out For now, assume that sum fits in n-bit 2’s comp. representation Assuming 8-bit 2’s complement numbers Ignore carry. ?? (104) (-10) (-16) (-9) (88) (-19) carry(discard)

21 Subtraction - 00010000 (16) - (-9) + 11110000 (-16) + (9)
Negate 2nd operand and add Assume all integers have the same number of bits Ignore carry out For now, assume that difference fits in n-bit 2’s comp. representation (104) (-10) (16) - (-9) (-16) + (9) (88) (-1) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Assuming 8-bit 2’s complement numbers.

22 Logical Operations Operations on logical TRUE or FALSE
Two states: TRUE=1, FALSE=0 View n-bit number as a collection of n logical values Operation applied to each bit independently 1 A A AND B B 1 A A OR B B 1 A NOT A

23 Examples of Logical Operations
AND AND Useful for clearing bits AND with zero = 0 AND with one = no change OR Useful for setting bits OR with zero = no change OR with one = 1 NOT Unary operation -- one argument Flips every bit OR NOT

24 Overflow + 01001 (9) + 10111 (-9) 10001 (-15) 01111 (+15)
Overflow is said to occur if result is too large to fit in the number of bits used in the representation. Sum cannot be represented as n-bit 2’s comp number We have overflow if Signs of both numbers are the same, and Sign of sum is different If Positive number is subtracted from a Negative number, result is positive and vice versa 01000 (8) (-8) (9) (-9) 10001 (-15) (+15) Question: Can we have overflow when adding numbers with opposite sign? No, sum will never be more positive nor more negative than both the operands. Question: Why does carry into MSB have to equal carry out? Just a shortcut to test sign of result differs from signs of operands. If both 0, result only 1 if carry-in is 1, no carry out (overflow). If both 1, result 0 if carry-in is 0, so carry-out is 1 (overflow). If one 0 and other 1, carry-in always results in carry-out being 1.

25 Number System People like to use decimal numbers
Computers use binary numbers Java translates decimal numbers into binary The computer does all its arithmetic in binary Java translates binary results back into decimal You occasionally have to use numbers in other number systems In Java, you can write numbers as octal, decimal, or hexadecimal (but not binary) Colors are usually specified in hexadecimal notation: #FF0000, #669966,

26 Hexadecimal Notation It is often convenient to write binary (base-2) numbers as hexadecimal (base-16) numbers instead Fewer digits: four bits per hex digit Less error prone: easy to corrupt long string of 1’s and 0’s 7 6 5 4 3 2 1 Hex 0111 0110 0101 0100 0011 0010 0001 0000 Decimal Binary F E D C B A 9 8 Hex 15 1111 14 1110 13 1101 12 1100 11 1011 10 1010 1001 1000 Decimal Binary Why is hexadecimal so convenient for representing binary numbers? 4 binary digits represent 16 values, the same as 1 hex digit; not true of, e.g., decimal.

27 Hexadecimal Conversions
Binary to Hex Every group of four bits is a hex digit Start grouping from right-hand side 7 D 4 F 8 A 3 What if we group from right to left? Hex to Decimal 1AC16 = 1 x x x 160 = 42810 This is not a new machine representation, just a convenient way to write the number.

28 Octal Numbers 3 digits per every octal group Octal Decimal Binary 7 6
5 4 3 2 1 Octal 111 110 101 100 011 010 001 000 Decimal Binary

29 Octal to Binary Conversion
One octal digit equals three binary digits Octal to Decimal 1738 = 1 x x x 80 = 12310

30 Fractions: Fixed-Point
How can we represent fractions? Use a “binary point” to separate positive from negative powers of two (just like “decimal point”) 2’s comp addition and subtraction still work If binary points are aligned (40.625) (-1.25) (39.375) 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 No new operations -- same as integer arithmetic

31 Very Large and Very Small: Floating-Point
Problem Large values: x requires 79 bits Small values: x requires >110 bits Use equivalent of “scientific notation”: F x 2E Need to represent F (fraction), E (exponent), and sign IEEE 754 Floating-Point Standard (32-bits): Note: Avagadro’s number and Planck’s constant Special casing exponent = 0, allows for the representation of 0. 1b 8b 23b S Exponent Fraction

32 Floating Point Example
Single-precision IEEE floating point number Sign is 1: number is negative Exponent field is = 126 (decimal) Fraction is … = 2-1 = 1/2 = 0.5 (decimal) Value = -1.5 x 2( ) = -1.5 x 2-1 = -0.75 sign exponent fraction

33 Text: ASCII Characters
ASCII: Maps 128 characters to 7-bit code. Both printable and non-printable (ESC, DEL, …) characters del 7f o 6f _ 5f O 4f ? 3f / 2f us 1f si 0f ~ 7e n 6e ^ 5e N 4e > 3e . 2e rs 1e so 0e } 7d m 6d ] 5d M 4d = 3d - 2d gs 1d cr 0d | 7c l 6c \ 5c L 4c < 3c , 2c fs 1c np 0c { 7b k 6b [ 5b K 4b ; 3b + 2b esc 1b vt 0b z 7a j 6a Z 5a J 4a : 3a * 2a sub 1a nl 0a y 79 i 69 Y 59 I 49 9 39 ) 29 em 19 ht 09 x 78 h 68 X 58 H 48 8 38 ( 28 can 18 bs 08 w 77 g 67 W 57 G 47 7 37 ' 27 etb 17 bel 07 v 76 f 66 V 56 F 46 6 36 & 26 syn 16 ack 06 u 75 e 65 U 55 E 45 5 35 % 25 nak 15 enq 05 t 74 d 64 T 54 D 44 4 34 $ 24 dc4 14 eot 04 s 73 c 63 S 53 C 43 3 33 # 23 dc3 13 etx 03 r 72 b 62 R 52 B 42 2 32 " 22 dc2 12 stx 02 q 71 a 61 Q 51 A 41 1 31 ! 21 dc1 11 soh 01 p 70 ` 60 P 50 @ 40 30 sp 20 dle 10 nul 00 American Standard Code for information Interchange Java uses Unicode Standard for characters (

34 Storage Space for Numerics
Numeric types in Java are characterized by their size: how much memory they occupy 1 byte = 8 bits Integral types Floating point types size range byte 1 byte -128: 127 short 2 bytes -32768:32767 char 0:65535 int 4 bytes : long 8 bytes smallest > 0 4.9E-324 1.4E-45 1.7E308 8 bytes double 3.4E38 4 byte float largest size

35 Other Data Types Text strings Image
Sequence of characters, terminated with NULL (0) Image Array of pixels Monochrome: one bit (0/1 = black/white) Color: red, green, blue (RGB) components (e.g., 8 bits each)


Download ppt "Introduction to Programming with Java, for Beginners"

Similar presentations


Ads by Google