Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 1 1 1 1 1.

Similar presentations


Presentation on theme: "1 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 1 1 1 1 1."— Presentation transcript:

1

2 1 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 1 1 1 1 1

3 A text code is a system that uses binary numbers (1s and 0s) to represent characters understood by humans (letters and numerals). An early text code system, called EBCDIC, uses eight- bit codes, but is used primarily in older mainframe systems. In the most common text-code set, ASCII, each character consists of eight bits (one byte) of data. ASCII is used in nearly all personal computers. In the Unicode text-code set, each character consists of 16 bits (two bytes) of data. How Computers Represent Data - Text Codes

4 Code CodeCharacter 00110000 0 00110001 1 00110010 2 00110011 3 00110100 4 00110101 5 01000001 A 01000010 B 01000011 C 01000100 D 01000101 E Examples from the ASCII Text Code Text Codes

5 Goal: Representing Numbers in Binary b Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2, 3 … 9 b Base 2 (binary) - Numbers are represented using 2 numerals: 0, 1 b Base 10 - Each position represents a power of 10: 101 = 1*10 2 + 0*10 1 + 1*10 0 = 100 + 1 110 = 1*10 2 + 1*10 1 + 0*10 0 = 100 + 10 b Base 2 - Each position represents a power of 2: 101 b = 1*2 2 + 0*2 1 + 1*2 0 = 100 b + 1 b = 4 + 1 110 b = 1*2 2 + 1*2 1 + 0*2 0 = 100 b + 10 b = 4+2

6 b Numbers in base 10 are called decimal numbers, they are composed of 10 numerals. d 3 d 2 d 1 d 0 = d 3 *10 3 + d 2 *10 2 + d 1 *10 1 + d 0 *10 0 9786 = 9*1000 + 7*100 + 8*10 + 6*1 = 9*10 3 + 7*10 2 + 8*10 1 + 6*10 0 b Numbers in base 2 are called binary numbers, they are composed of the numerals 0 and 1. b 3 b 2 b 1 b 0 = b 3 *2 3 + b 2 *2 2 + b 1 *2 1 + b 0 *2 0 110101 = 1*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1*1 = 1*2 5 + 1*2 4 + 0*2 3 + 1*2 2 + 0*2 1 + 1*2 0  1111 1111 1111 1111 = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 2 15 + 2 14 + … + 2 1 + 2 0 =  2 n = 2 n+1 - 1 = 2 16 -1  1111 1111 1111 1111 = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 2 15 + 2 14 + … + 2 1 + 2 0 =  2 n = 2 n+1 - 1 = 2 16 -1

7 Converting from Binary to Decimal b Easy: Multiply each numeral by its exponent. b 1001 b = 1*2 3 + 1*2 0 = 1*8 + 1*1 = 9 d b 0111 b = 0*2 3 + 1*2 2 + 1*2 1 + 1*2 0 = 100 b + 10 b + 1 b = 4 + 2 + 1 = 7 b 110110 b = 1*2 5 + 1*2 4 + 0*2 3 + 1*2 2 + 1*2 1 = 100000 b + 10000 b + 100 b + 10 b = 32 + 16 + 4 + 2 = 54

8  Given an array that holds a 32 digit binary number called binary: b int[] binary = {1, 0, 0, 1}; int decimal = 0; for(int i=binary.length-1;i>=0;i--) decimal += binary[i]*Math.pow(2,i); decimal += binary[i]*Math.pow(2,i); b The powers of 2: 2 0 = 12 1 = 22 2 = 42 3 = 8 2 4 = 162 5 = 322 6 = 642 7 = 128 2 8 = 2562 9 = 5122 10 = 1024 = 1K 2 11 = 20482 12 = 4096 2 13 = 8192 2 16 = 655362 20 = 1048576 = 1M Converting from Binary to Decimal

9 Converting From Decimal to Binary In Java it looks like this: int decimal = readInt(); int res; int power; for(int n=31; n>=0; n--){ power = (int)Math.pow(2,n); power = (int)Math.pow(2,n); res = decimal/power; res = decimal/power; decimal = decimal - res*power; decimal = decimal - res*power; System.out.print(res); System.out.print(res);}

10 A Base Conversion Example 500/2 9 = 0 ; 500 - 0 = 500 4/2 3 = 0 ; 4 - 0 = 4 500/2 8 = 1 ; 500 - 256 = 244 4/2 2 = 1 ; 4 - 4 = 0 244/2 7 = 1 ; 244 - 128 = 116 0/2 1 = 0 ; 0 - 0 = 0 116/2 6 = 1 ; 116 - 64 = 52 0/2 0 = 0 ; 0 - 0 = 0 52/2 5 = 1 ; 52 - 32 = 20 52/2 5 = 1 ; 52 - 32 = 20 20/2 4 = 1 ; 20 - 16 = 4 20/2 4 = 1 ; 20 - 16 = 4 500 d = 111110100 b

11 Representation of Numbers  int, long, short, char - positive numbers are stored as a binary number where the MSB (Most Significant Bit) is 0 A short is represented by 16 bits (2 bytes) 100d = 2 6 + 2 5 + 2 2 = 0000000001100100 An int is represented by 32 bits (4 bytes) 65545 d = 2 16 + 2 3 + 2 0 = 00000000000000010000000000001001 A char is represented by 16 bits (2 bytes) ‘0’ = 48d = 2 5 + 2 4 = 00000000 00110000

12 Signed and Unsigned Numbers b How do we distinguish between them? b Solution: Use 1 bit to represents the sign. The name of this scheme is called: sign and magnitude 10001111 b = -15 00001111 b = +15 (8 bit numbers) b There are several problems with this scheme: where do we put the sign?where do we put the sign? an extra step is needed to calculate the sign bitan extra step is needed to calculate the sign bit – Add the magnitudes. – Calculate the sign. there is both a positive and negative zero 10000000 b = -0 00000000 b = +0there is both a positive and negative zero 10000000 b = -0 00000000 b = +0

13 Two's Complement b New Solution: Leading 0s mean positive and leading 1s mean negative. 01111111 b > 0 10000000 b 0 10000000 b < 0 (8 bit numbers) b The largest positive number is: 2,147,483,647 d (2 31 -1) = 01111…11111 b b The smallest negative number is: -2,147,483,648 d (-2 31 ) = 10000…00000 b b It is followed by -2,147,483,647 d (1000…0001 b ) up to -1 (1111…1111 b ). b The sum of a number and its inverse is 100...0, where 1 is an overflow and is thrown away.

14 Two’s Complement b +3 = 00000011 b +2 = 00000010 b +1 = 00000001 b +0 = 00000000 b -1 = 11111111 b -2 = 11111110 b -3 = 11111101

15 Benefits b One representation of zero b Arithmetic works easily (see later) b Negating is fairly easy 3 = 000000113 = 00000011 Boolean complement gives11111100Boolean complement gives11111100 Add 1 to LSB11111101Add 1 to LSB11111101

16 Geometric Depiction of Twos Complement Integers

17 Negative Decimal to Binary b Translate the number to binary. b Flip the bits (1 -> 0, 0 -> 1) b Add 1 b -100 d = -01100100 = 10011011 + 1 = 10011100 -1 d = - 00000001 = 11111110 + 1 = 11111111 b -128 d = -10000000 = 01111111 + 1 = 10000000 b 11000101 b = 00111010 + 1 = 00111011 = -59 d b In a short: -25,000 d = -0110000110101000 = 1001111001010111 + 1 = 10011110011000 b

18 Conversion Between Lengths b Positive numbers pack with leading zeros b +18 = 00010010 b +18 = 00000000 00010010 b Negative numbers pack with leading ones b -18 = 10010010 b -18 = 11111111 10010010 b i.e. pack with MSB (sign bit)

19 Hexadecimal Numbers b Numbers in base 16 are called hexadecimal numbers, they are composed of 16 numerals (0-9,a-f). 9786 hex = 9*16 3 + 7*16 2 + 8*16 1 + 6*16 0 = = 9*4096 + 7*256 + 8*16 + 6*1 = 38790 d 0xabcdef = 10*16 5 + 11*16 4 + 12*16 3 + 13*16 2 + 14*16 1 + 15*16 0 = 11259375 d b The conversion from binary to hex is very easy, each hex digit is 4 binary digits: 0x0 = 0000 0x1 = 0001 0x2 = 0010 0x3 = 0011 0x4 = 0100 0x5 = 0101 0x6 = 0110 0x7 = 0111 0x8 = 1000 0x9 = 1001 0xa = 1010 0xb = 1011 0xc = 1100 0xd = 1101 0xe = 1110 0xf = 1111

20 Binary Hexadecimal b Using the previous table it is easy to represent 32 bit binary numbers in a short form: 0100 1010 0010 1101 1000 1100 0111 1001 = 4 a 2 d 8 c 7 9 = 0x4a2d8c79. b 0x12390ab4 = 0001 0010 0011 1001 0000 1010 1011 0100 1 2 3 9 0 a b 4 b 0xffffffff = 1111 1111 1111 1111 1111 1111 1111 1111 b Every 2 Hex digits are a byte

21 b Conversion from decimal to hexadecimal is similar to converting decimal to binary. int decimal=readInt(); int res; int power; for(int n=7;n>=0;n--){ power = (int)Math.pow(16,n); power = (int)Math.pow(16,n); res = decimal/power; res = decimal/power; decimal = decimal - res*power; if(res>9) System.out.print((char)((res - 10) + 'a')); decimal = decimal - res*power; if(res>9) System.out.print((char)((res - 10) + 'a')); else else System.out.print(res); System.out.print(res);} Decimal -> Hexadecimal

22 b Java allows numbers in octal (start with 0) and hexadecimal (start with 0x or 0X). int num1 = 015; int num2 = 0x1a; num1 is 13 num2 is 26 Octal & Hex in Java

23 Addition b Lets add 6 to 7: 00…00111 = 7 d 00…00110 = 6 d 00…01101 = 13 d b Lets add 11 to 5: 00…01011 = 11 d 00…00101 = 5 d 00…10000 = 16 d

24 Substraction b Subtraction uses addition, the operand is simply negated before being added: 7 - 6 = 7 + (-6) = 00…00111 = 7 d 11…11010 = -6 d 00…00001 = 1 d

25 Overflow b Overflow occurs when the result of a operation can't be represented by the hardware. This can happen when we add two numbers of the same sign or subtract two large numbers of opposing signs. b Overflow is detected by the following table: Operation A B Result A+B >=0 >=0 =0 A- B >=0 =0 >=0

26 Multiplication b Look at the following pencil and paper example: 1000 d X 1001 d 1000 0000 0000 1000 1001000 d b By restricting the digits to 1 & 0 (which is binary) the algorithm is simple, at each step: Place a copy of the multiplicand if the multiplier digit is 1.Place a copy of the multiplicand if the multiplier digit is 1. Place 0 if the digit is 0.Place 0 if the digit is 0. The position for the next step is shifted left by one place.The position for the next step is shifted left by one place.

27 Real Numbers b Numbers with fractions b Could be done in pure binary 1001.1010 = 2 4 + 2 0 +2 -1 + 2 -3 =9.6251001.1010 = 2 4 + 2 0 +2 -1 + 2 -3 =9.625 b Where is the binary point? b Fixed? Very limitedVery limited b Moving? How do you show where it is?How do you show where it is?

28 Floating Point b +/-.significand x 2 exponent b Point is actually fixed between sign bit and body of mantissa b Exponent indicates place value (point position) Sign bit Biased Exponent Significand or Mantissa

29 Signs for Floating Point b Mantissa is stored in 2s compliment b Exponent is in excess or biased notation e.g. Excess (bias) 128 meanse.g. Excess (bias) 128 means 8 bit exponent field8 bit exponent field Pure value range 0-255Pure value range 0-255 Subtract 128 to get correct valueSubtract 128 to get correct value Range -128 to +127Range -128 to +127

30 Normalization b FP numbers are usually normalized b i.e. exponent is adjusted so that leading bit (MSB) of mantissa is 1 b Since it is always 1 there is no need to store it b (c.f. Scientific notation where numbers are normalized to give a single digit before the decimal point b e.g. 3.123 x 10 3 )

31 Floating Point Examples X X X X

32 Expressible Numbers

33 IEEE 754 b Standard for floating point storage b 32 and 64 bit standards b 8 and 11 bit exponent respectively b Extended formats (both mantissa and exponent) for intermediate results

34 FP Arithmetic +/- b Check for zeros b Align significands (adjusting exponents) b Add or subtract significands b Normalize result

35 FP Arithmetic x/  b Check for zero b Add/subtract exponents b Multiply/divide significands (watch sign) b Normalize b Round b All intermediate results should be in double length storage


Download ppt "1 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 1 1 1 1 1."

Similar presentations


Ads by Google