460 likes | 636 Views
مبانی کامپیوتر و برنامه سازی. Bits, Data types, and Operations. Data Types. Numbers Signed: -10, 5, 20, -123, … Unsigned: 10, 120, 2… Integers: 1, 2, 3, … Floating point: 2x10 -12 , 6.23x10 4 , … Complex: 2+j1 4.25+J6.23, … Rational: 2.3456, 10.239, … Text Characters: a, b, c, …
E N D
مبانی کامپیوتر و برنامه سازی Bits, Data types, and Operations
Data Types • Numbers • Signed: -10, 5, 20, -123, … • Unsigned: 10, 120, 2… • Integers: 1, 2, 3, … • Floating point: 2x10-12, 6.23x104 , … • Complex: 2+j1 4.25+J6.23, … • Rational: 2.3456, 10.239, … • Text • Characters: a, b, c, … • Strings: “Tehran University”, “Bahman”, … مبانی کامپیوتر و برنامه سازی
Data Types • Images: • Pixels: • Shapes: • Sound • Logical • True: “3>2” • False: “6-3=5” • Instructions • Add two numbers, multiply 3 by 2, … مبانی کامپیوتر و برنامه سازی
329 102 101 100 3x100 + 2x10 + 9x1 = 329 Decimal Numbers • “decimal” means that we have ten digits to use in our representation (the symbols 0 through 9) • Decimal number 3,546? • it is threethousands plus fivehundreds plus fourtens plus six ones. • Decimal number: 329: مبانی کامپیوتر و برنامه سازی
Data representation in computers • At the lowest level, computers are constructed by a bunch of simple switches. • A computer is an electronic machine, and the switches control the flow of electrons. مبانی کامپیوتر و برنامه سازی
Data representation • Easy to recognize two conditions: • Presence of a voltage – we’ll call this state “1” • Absence of a voltage – we’ll call this state “0” • Could base state on value of voltage, but control and detection circuits more complex. • compare turning on a light switch tomeasuring or regulating voltage مبانی کامپیوتر و برنامه سازی
Digital data • To make things even simpler we don’t count on absolute presence or absence but the closeness. • Digital system: • finite number of symbols مبانی کامپیوتر و برنامه سازی
Symbols • One bit has 2 possible states: 0, 1 • Two bits have 4 possible states: 2*2= 22 = 4 مبانی کامپیوتر و برنامه سازی
Symbols • Three bits have 8 possible states: 2*2*2= 23 = 8 مبانی کامپیوتر و برنامه سازی
Symbols • N Bits have 2N states: • To represent K states we need: 2 * …*2*2= 2N K= 2N N = log2 K مبانی کامپیوتر و برنامه سازی
most significant least significant 101 22 21 20 1x4 + 0x2 + 1x1 = 5 Binary Data • Binary (base two) system: • has two states: 0 and 1 • Basic unit of information is the binary digit, or bit. • Binary number 101 مبانی کامپیوتر و برنامه سازی
Binary Conversion • Binary to Decimal Binary an….a2a1a0 = Decimal an*2n+…. +a2*22+a1*21+a0*20 Example: 101101B = 1*25+0*24 +1*23 +1*22+0*21+1*20 = 32+0+8+4+0+1 = 45 D مبانی کامپیوتر و برنامه سازی
Binary Conversion • Decimal to Binary • Divide by two – remainder is least significant bit. • Keep dividing by two until answer is zero,writing remainders from right to left. Example: X = 104D 104/2 = 52 r: 0 bit 0 52/2 = 26 r: 0 bit 1 26/2 = 13 r: 0 bit 2 13/2 = 6 r: 1 bit 3 6/2 = 3 r: 0 bit 4 3/2 = 1 r: 1 bit 5 1/2 = 0 r: 1 bit 6 X = 1101000B مبانی کامپیوتر و برنامه سازی
carry 10010 10010 1111 + 1001 + 1011 + 1 11011 11101 10000 10111 + 111 Unsigned Addition • Just like Decimal numbers add from right to left, propagating carry: مبانی کامپیوتر و برنامه سازی
Binary range • With N bits we can represent numbers in the range of: 0 i 2N - 1 • Problem: How to represent negative numbers? مبانی کامپیوتر و برنامه سازی
Signed Magnitude • In decimal we use a ‘-’ in front of a number to indicate negative. • In binary we can use a leading bit to represent sign: • 0: Positive • 1: Negative Example: 0 1101B = + (1*23 + 1*22 +0*21 +1*20) =13D 1 1101B = - (1*23 + 1*22 +0*21 +1*20) = -13D مبانی کامپیوتر و برنامه سازی
Signed Magnitude • Problems: • We have two numbers for zero (+/-)! • How do we do addition/subtraction? مبانی کامپیوتر و برنامه سازی
One’s Complement • The leading bit is used to represent sign: • 0: Positive • 1: Negative • When negative invert all the bits Example: 0 1101B = + (1*23 + 1*22 +0*21 +1*20) =13D 1 1101B = - (0*23 + 0*22 +1*21 +0*20) = -2D مبانی کامپیوتر و برنامه سازی
One’s Complement • Problems: • We have two numbers for zero (+/-)! • How do we do addition/subtraction? مبانی کامپیوتر و برنامه سازی
Two’s Complement • The leading bit is used to represent sign: • 0: Positive • 1: Negative • When negative invert all the bits and add 1 to the result Example: 0 1101B = + (1*23 + 1*22 +0*21 +1*20) =13D 1 1101B = - (0*23 + 0*22 +1*21 +0*20 + 1) = -3D مبانی کامپیوتر و برنامه سازی
Two’s Complement Advantages: • Only one representation for zero • Operations need not check the sign • Efficient use of all the bits مبانی کامپیوتر و برنامه سازی
Two’s complement addition 00101 (5)01001 (9) + 11011(-5)+10111(-9) 00000 (0) 00000(0) مبانی کامپیوتر و برنامه سازی
Binary Conversion • Binary to Decimal conversion of signed numbers, for an….a2a1a0: • If an = 0, the magnitude of the number is: an-1….a2a1a0 = an-1*2n-1+…. +a2*22+a1*21+a0*20 • if an = 1, obtain the 2’s complement of the number. This will represent the magnitude. مبانی کامپیوتر و برنامه سازی
Binary Conversion • Decimal to Binary conversion of signed numbers: • Divide by two – remainder is least significant bit. • Keep dividing by two until answer is zero,writing remainders from right to left. • If the original number is positive append a zero • If the original number is negative obtain the 2’s complement of the result and append it with a 1. مبانی کامپیوتر و برنامه سازی
Arithmetic Operations • Addition • All numbers should have the same number of bits • Ignore carry out 01101000(104) 11110110(-10) + 00010000( 16) +11110111 (-9) 01111000(120) 11101101(-19) مبانی کامپیوتر و برنامه سازی
Arithmetic Operations • Subtraction • All numbers should have the same number of bits • Negate subtrahend (2nd no.) and add. • ignore carry out 01101000 (104) - 00010000(16) 01101000 (104) + 11110000(-16) 01011000 (88) مبانی کامپیوتر و برنامه سازی
Sign extension • To add two numbers, we must represent them with the same number of bits. • If we just pad with zeroes on the left: • Instead, replicate the MS bit -- the sign bit: 4-bit8-bit 0100 (4)00000100 (still 4) 1100 (-4)00001100 (12, not -4) 4-bit8-bit 0100 (4)00000100 (still 4) 1100 (-4)11111100 (still -4) مبانی کامپیوتر و برنامه سازی
Number Range • Unsigned representation 0 i < 2N - 1 • Signed Magnitude -2N-1 + 1 < i < 2N-1 - 1 • 1’s Complement -2N-1 + 1 < i < 2N-1 - 1 • 2’s Complement -2N-1 < i < 2N-1 - 1 مبانی کامپیوتر و برنامه سازی
Overflow • If operands are too big, then sum cannot be represented as an n-bit 2’s comp number. • We have overflow if: • signs of both operands are the same, and • sign of sum is different. • Another test -- easy for hardware: • carry into MS bit does not equal carry out 01000 (8)11000 (-8) + 01001(9)+ 10111(-9) 10001 (-15) 01111(+15) مبانی کامپیوتر و برنامه سازی
0.329 0.011 10-1 2-1 10-2 2-2 10-3 2-3 0x0.5 + 1x0.25 + 1x0.125 = 0.375 3x0.1 + 2x0.01 + 9x0.001 = 329 Fractional Values • In Decimal: • In Binary مبانی کامپیوتر و برنامه سازی
2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 00101000.101 (40.625) + 11111110.110(-1.25) 00100111.011 (39.375) Fractional Operations • 2’s comp addition and subtraction still work if binary points are aligned مبانی کامپیوتر و برنامه سازی
Fractional Values • Fixed point notation 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) مبانی کامپیوتر و برنامه سازی
Floating Point Representation 1 8 bits 23 bits s fraction exponent • Represent all exponent values as unsigned integers • Example: 10111111010000000000000000000000 • Sign is 1 – number is negative. • Exponent field is 01111110 = 126 (decimal). • Fraction is 0.100000000000… = 0.5 (decimal). • Value = -1.5 x 2(126-127) = -1.5 x 2-1 = -0.75. N = (-1)s x 1.fraction x 2(exponent – 127) sign exponent fraction مبانی کامپیوتر و برنامه سازی
Floating Point • Values represented by convention: • Infinity (+ and -): exponent = 255 and fraction = 0 • NaN (not a number): exponent = 255 and fraction 0 • Zero (0): exponent = 0 and fraction = 0 • Exponent = 0 => fraction is de-normalized: N = (-1)s x 0.fraction x 2(exponent – 127) مبانی کامپیوتر و برنامه سازی
Double precision floating point 11 bits 1 52 bits exponent s fraction N = (-1)s x 1.fractionx 2(exponent – 1023) مبانی کامپیوتر و برنامه سازی
Hexadecimal • 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 • 0 to 9 represented as such • 10, 11, 12, 13, 14, 15 represented by A, B, C, D, E, F • 16 = 24: i.e. every hexadecimal digit can be represented by a 4-bit binary (unsigned) and vice-versa. مبانی کامپیوتر و برنامه سازی
Hexadecimal • Every four bits is a hex digit. • start grouping from right-hand side 011101010001111010011010111 3 A 8 F 4 D 7 This is not a new machine representation,just a convenient way to write the number. مبانی کامپیوتر و برنامه سازی
Hexadecimal مبانی کامپیوتر و برنامه سازی
Logical Operations • 0, 1 in a binary value can represent logical TRUE = 1, or FALSE = 0. • We can perform logical operations on binary bits or a set of binary bits, also known as Boolean algebra. • The basic operations are AND, OR, NOT مبانی کامپیوتر و برنامه سازی
Basic Logic Operations • Truth Tables of Basic Operations • Equivalent Notations • Not A = A’ = A • A and B = A.B = AB = A intersection B • A or B = A+B = AB = A union B مبانی کامپیوتر و برنامه سازی
More Logic Operations • XOR and XNOR مبانی کامپیوتر و برنامه سازی
Logical Operations Example 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 NOT11000101 00111010 مبانی کامپیوتر و برنامه سازی
Text Representation • ASCII: Maps 128 characters to 7-bit code. • both printable and non-printable (ESC, DEL, …) characters مبانی کامپیوتر و برنامه سازی
Other Data Types • Text strings • sequence of characters, terminated with NULL (0) • typically, no hardware support • Image • array of pixels • monochrome: one bit (1/0 = black/white) • color: red, green, blue (RGB) components (e.g., 8 bits each) • other properties: transparency • Sound • sequence of fixed-point numbers مبانی کامپیوتر و برنامه سازی