Introduction to Programming with Java, for Beginners

Slides:



Advertisements
Similar presentations
Representing Numbers: Integers
Advertisements

Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Data Representation Computer Organization &
Chapter 2 Bits, Data Types, and Operations. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Wael Qassas/AABU.
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java

Assembly Language and Computer Architecture Using C++ and Java
Data Representation ICS 233
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Number Systems Lecture 02.
Chapter 2 Bits, Data Types, and Operations. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-2 How do we.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
Simple Data Type Representation and conversion of numbers
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
IT253: Computer Organization
Data Representation and Computer Arithmetic
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Bits, Data Types, and Operations. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-2 How do we represent.
Number Systems Spring Semester 2013Programming and Data Structure1.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
PART 2 Data Types 1. Integers 2. 2’s Complement 3. Conversion
ECEN 301Discussion #19 – Binary Numbers1 Numbered Moses 1:33,35,37 33 And worlds without number have I created; and I also created them for mine own purpose;
Chapter 2 Bits, Data Types, and Operations. 2-2 Hexadecimal Notation It is often convenient to write binary (base-2) numbers as hexadecimal (base-16)
Bits, Data Types, and Operations Slides based on set prepared by Gregory T. Byrd, North Carolina State University.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Chapter 2 Bits, Data Types, and Operations. 2-2 How do we represent data in a computer? At the lowest level, a computer is an electronic machine. works.
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)
Data Representation, Number Systems and Base Conversions
AEEE2031 Data Representation and Numbering Systems.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science,
Instructor:Po-Yu Kuo 教師:郭柏佑
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Data Representation. How is data stored on a computer? Registers, main memory, etc. consists of grids of transistors Transistors are in one of two states,
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Data Representation COE 308 Computer Architecture
Invitation to Computer Science, C++ Version, Fourth Edition
Bits, Data Types, and Operations
Chapter 2 Bits, Data Types, and Operations
Programming and Data Structure
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Number Representation
Data Representation Binary Numbers Binary Addition
Invitation to Computer Science, Java Version, Third Edition
Chapter 2 Bits, Data Types, and Operations
Data Representation COE 301 Computer Organization
Chapter 2 Bits, Data Types, and Operations
Introduction to Computer Engineering
School of Computer Science and Technology
Data Representation ICS 233
Introduction to Computer Engineering
Data Representation COE 308 Computer Architecture
Chapter 2 Bits, Data Types, and Operations
Presentation transcript:

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

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

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…

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. 00101001 can be 1. Decimal value 41 2. Alphabet (or character) ‘A’ in ASCII notation 3. Command to be performed e.g. Performing Add operation

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…

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) 3x100 + 2x10 + 9x1 = 329

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

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

Signed Integers Positive integers Negative integers Just like unsigned with zero in most significant bit 00101 = 5 Negative integers Sign-magnitude: set 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 Indus Valley Civilization (Pakistan) “discovered” negative number c 2600BC. (India “discovered” 0 about the same time.)

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

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) 01001 (9) + 11011 (-5) + (-9) 00000 (0) 00000 (0) 1 1 1 1

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) 01001 (9) 11010 (1’s comp) 10110 (1’s comp) + 1 + 1 11011 (-5) 1 1 1 1 (-9)

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

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 = 01101000two = 26+25+23 = 64+32+8 = 104ten Assuming 8-bit 2’s complement numbers.

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

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 = 01101000two

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 104 - 64 = 40 bit 6 40 - 32 = 8 bit 5 8 - 8 = 0 bit 3 X = 01101000two

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) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4) 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4)

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

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. ?? 01101000 (104) 11110110 (-10) + 11110000 (-16) + 11110111 (-9) 1 01011000 (88) (-19) 1 1 1 1 0 1 1 0 1 carry(discard)

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 01101000 (104) 11110110 (-10) - 00010000 (16) - (-9) + 11110000 (-16) + (9) 01011000 (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.

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

Examples of Logical Operations 11000101 AND 00001111 00000101 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 11000101 OR 00001111 11001111 NOT 11000101 00111010

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) 11000 (-8) + 01001 (9) + 10111 (-9) 10001 (-15) 01111 (+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.

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,

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.

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

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

Octal to Binary Conversion One octal digit equals three binary digits 101101011100101000001011 5 5 3 4 5 0 1 3 Octal to Decimal 1738 = 1 x 82 + 7 x 81 + 3 x 80 = 12310

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 00101000.101 (40.625) + 11111110.110 (-1.25) 00100111.011 (39.375) 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 No new operations -- same as integer arithmetic

Very Large and Very Small: Floating-Point Problem Large values: 6.023 x 1023 -- requires 79 bits Small values: 6.626 x 10-34 -- 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

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

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 (http://en.wikipedia.org/wiki/Unicode)

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 -2147483648:2147483647 long 8 bytes … smallest > 0 4.9E-324 1.4E-45 1.7E308 8 bytes double 3.4E38 4 byte float largest size

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)