number representation number representation
play

Number Representation Number Representation CS10001: Programming - PowerPoint PPT Presentation

Number Representation Number Representation CS10001: Programming & Data Structures Programming & Data Structures CS10001: Pallab Dasgupta Dasgupta Pallab Professor, Dept. of Computer Sc. & Engg Engg., ., Professor, Dept. of


  1. Number Representation Number Representation CS10001: Programming & Data Structures Programming & Data Structures CS10001: Pallab Dasgupta Dasgupta Pallab Professor, Dept. of Computer Sc. & Engg Engg., ., Professor, Dept. of Computer Sc. & Indian Institute of Technology Kharagpur Kharagpur Indian Institute of Technology Dept. of CSE, IIT KGP

  2. Topics to be Discussed Topics to be Discussed How are numeric data items actually stored in computer memory? How are numeric data items actually stored in computer memory? • • How much space (memory locations) is allocated for each type of How much space (memory locations) is allocated for each type of • • data? data? – int int, float, char, etc. , float, char, etc. – How are characters and strings stored in memory? How are characters and strings stored in memory? • • Dept. of CSE, IIT KGP

  3. Number System :: The Basics Number System :: The Basics We are accustomed to using the so- -called called decimal number decimal number We are accustomed to using the so • • system . . system Ten digits :: 0,1,2,3,4,5,6,7,8,9 Ten digits :: 0,1,2,3,4,5,6,7,8,9 – – Every digit position has a weight which is a power of 10. Every digit position has a weight which is a power of 10. – – Base or or radix radix is 10. is 10. Base – – Example: Example: 2 + 3 x 10 1 + 4 x 10 0 234 = 2 x 10 = 2 x 10 2 + 3 x 10 1 + 4 x 10 0 234 2 + 5 x 10 1 + 0 x 10 0 + 6 x 10 250.67 = 2 x 10 2 + 5 x 10 1 + 0 x 10 0 + 6 x 10 - -1 1 250.67 = 2 x 10 + 7 x 10 - -2 2 + 7 x 10 Dept. of CSE, IIT KGP

  4. Binary Number System Binary Number System Two digits: Two digits: • • 0 and 1. 0 and 1. – – Every digit position has a weight which is a power of 2. Every digit position has a weight which is a power of 2. – – Base or or radix radix is 2. is 2. Base – – Example: Example: • • 2 + 1 x 2 1 + 0 x 2 110 = 1 x 2 2 + 1 x 2 1 + 0 x 2 0 0 110 = 1 x 2 2 + 0 x 2 1 + 1 x 2 0 + 0 x 2 1 + 1 x 2 -1 -2 2 101.01 = 1 x 2 2 + 0 x 2 1 + 1 x 2 0 + 0 x 2 - + 1 x 2 - 101.01 = 1 x 2 Dept. of CSE, IIT KGP

  5. Binary- -to to- -Decimal Conversion Decimal Conversion Binary Each digit position of a binary number has a weight. Each digit position of a binary number has a weight. • • – Some power of 2. Some power of 2. – A binary number: A binary number: • • B = b n B = b 1 b b n 2 …..b …..b 1 1 b b 0 0 . b . b - 1 b b - 2 ….. b ….. b - n- -1 n- -2 -1 -2 -m m Corresponding value in decimal: Corresponding value in decimal: n-1 Σ D = Σ i b i 2 i D = b i 2 i = -m Dept. of CSE, IIT KGP

  6. Examples Examples 5 + 0x2 4 + 1x2 3 + 0x2 2 + 1x2 1 + 1x2 101011 � � 1x2 1x2 5 + 0x2 4 + 1x2 3 + 0x2 2 + 1x2 1 + 1x2 0 0 1. 1. 101011 = 43 = 43 (101011) 2 = (43) 10 (101011) 2 = (43) 10 1 + 1x2 2 + 0x2 3 + 1x2 .0101 � � 0x2 0x2 - -1 + 1x2 - -2 + 0x2 - -3 + 1x2 - -4 4 2. 2. .0101 = .3125 = .3125 (.0101) 2 = (.3125) 10 (.0101) 2 = (.3125) 10 2 + 0x2 1 + 1x2 0 + 1x2 1 + 1x2 101.11 � � 1x2 1x2 2 + 0x2 1 + 1x2 0 + 1x2 - -1 + 1x2 - -2 2 3. 3. 101.11 5.75 5.75 (101.11) 2 = (5.75) 10 (101.11) 2 = (5.75) 10 Dept. of CSE, IIT KGP

  7. Decimal- -to to- -Binary Conversion Binary Conversion Decimal Consider the integer and fractional parts Consider the integer and fractional parts • • separately. separately. For the integer part, For the integer part, • • – Repeatedly divide the given number by 2, and go on Repeatedly divide the given number by 2, and go on – accumulating the remainders, until the number becomes accumulating the remainders, until the number becomes zero. zero. – Arrange the remainders Arrange the remainders in reverse order . in reverse order . – For the fractional part, For the fractional part, • • – Repeatedly multiply the given fraction by 2. Repeatedly multiply the given fraction by 2. – • Accumulate the integer part (0 or 1). Accumulate the integer part (0 or 1). • • If the integer part is 1, chop it off. If the integer part is 1, chop it off. • – Arrange the integer parts Arrange the integer parts in the order in the order they are obtained. they are obtained. – Dept. of CSE, IIT KGP

  8. Example 1 :: 239 Example 1 :: 239 2 239 2 119 --- 1 2 59 --- 1 2 29 --- 1 2 14 --- 1 (239) 10 = (11101111) 2 2 7 --- 0 2 3 --- 1 2 1 --- 1 2 0 --- 1 Dept. of CSE, IIT KGP

  9. Example 2 :: 64 Example 2 :: 64 64 2 2 32 --- 0 2 16 --- 0 2 8 --- 0 2 4 --- 0 (64) 10 = (1000000) 2 2 2 --- 0 2 1 --- 0 2 0 --- 1 Dept. of CSE, IIT KGP

  10. Example 3 :: .634 Example 3 :: .634 .634 x 2 = 1.268 .268 x 2 = 0.536 .536 x 2 = 1.072 (.634) 10 = (.10100……) 2 .072 x 2 = 0.144 .144 x 2 = 0.288 : : Dept. of CSE, IIT KGP

  11. Example 4 :: 37.0625 Example 4 :: 37.0625 (37) 10 = (100101) 2 (.0625) 10 = (.0001) 2 ∴ (37.0625) 10 = (100101 . 0001) 2 Dept. of CSE, IIT KGP

  12. Hexadecimal Number System Hexadecimal Number System A compact way of representing binary numbers. A compact way of representing binary numbers. • • 16 different symbols (radix = 16). 16 different symbols (radix = 16). • • � 0000 � 1000 0 � 8 � 0000 1000 0 8 1 � � 0001 9 � � 1001 0001 1001 1 9 � 0010 � 1010 2 � A � 0010 1010 2 A � 0011 � 1011 3 � B � 3 0011 B 1011 � 0100 � 1100 4 � C � 0100 1100 4 C � 0101 � 1101 5 � D � 0101 1101 5 D 6 � � 0110 E � � 1110 6 0110 E 1110 � 0111 � 1111 7 � F � 0111 1111 7 F Dept. of CSE, IIT KGP

  13. Binary- -to to- -Hexadecimal Conversion Hexadecimal Conversion Binary For the integer part, For the integer part, • • – Scan the binary number from Scan the binary number from right to left . right to left . – – Translate each group of four bits into the corresponding Translate each group of four bits into the corresponding – hexadecimal digit. hexadecimal digit. • Add Add leading leading zeros if necessary. zeros if necessary. • For the fractional part, For the fractional part, • • – Scan the binary number from Scan the binary number from left to right left to right . . – – Translate each group of four bits into the corresponding Translate each group of four bits into the corresponding – hexadecimal digit. hexadecimal digit. • Add Add trailing trailing zeros if necessary. zeros if necessary. • Dept. of CSE, IIT KGP

  14. Example Example 1. 1. (1011 ( 1011 0100 0100 0011 0011) ) 2 = (B43) 16 = (B43) 2 16 2. (10 10 1010 1010 0001 0001) ) 2 = (2A1) 16 2. ( = (2A1) 2 16 3. 3. (.1000 (. 1000 010 010) ) 2 = (.84) 16 = (.84) 2 16 4. (101 101 . . 0101 0101 111 111) ) 2 = (5.5E) 16 4. ( = (5.5E) 2 16 Dept. of CSE, IIT KGP

  15. Hexadecimal- -to to- -Binary Conversion Binary Conversion Hexadecimal Translate every hexadecimal digit into its 4- Translate every hexadecimal digit into its 4 -bit binary bit binary • • equivalent. equivalent. Examples: Examples: • • (3A5) 16 = (0011 1010 0101) 2 (3A5) = (0011 1010 0101) 16 2 (12.3D) 16 = (0001 0010 . 0011 1101) 2 (12.3D) = (0001 0010 . 0011 1101) 16 2 (1.8) 16 = (0001 . 1000) 2 (1.8) = (0001 . 1000) 16 2 Dept. of CSE, IIT KGP

  16. Unsigned Binary Numbers Unsigned Binary Numbers An n- -bit binary number bit binary number An n • • B = b n B = b 1 b b n 2 …. b …. b 2 2 b b 1 1 b b 0 n- -1 n- -2 0 n distinct combinations are possible, 0 to 2 • 2 2 n distinct combinations are possible, 0 to 2 n n - -1. 1. • For example, for n = 3, there are 8 distinct combinations. For example, for n = 3, there are 8 distinct combinations. • • – 000, 001, 010, 011, 100, 101, 110, 111 000, 001, 010, 011, 100, 101, 110, 111 – Range of numbers that can be represented Range of numbers that can be represented • • � � 0 to 2 8 8 - n=8 n=8 0 to 2 -1 (255) 1 (255) � � 16 - n=16 0 to 2 16 -1 (65535) 1 (65535) n=16 0 to 2 � � 32 - n=32 0 to 2 32 -1 (4294967295) 1 (4294967295) n=32 0 to 2 Dept. of CSE, IIT KGP

  17. Signed Integer Representation Signed Integer Representation Many of the numerical data items that are used in a program are Many of the numerical data items that are used in a program are • • signed (positive or negative). signed (positive or negative). – Question:: How to represent sign? Question:: How to represent sign? – Three possible approaches: Three possible approaches: • • – Sign Sign- -magnitude representation magnitude representation – – One’s complement representation One’s complement representation – – Two’s complement representation Two’s complement representation – Dept. of CSE, IIT KGP

  18. Sign- -magnitude Representation magnitude Representation Sign For an n- For an n -bit number representation bit number representation • • – The most significant bit (MSB) indicates sign The most significant bit (MSB) indicates sign – � positive 0 � positive 0 � negative 1 � negative 1 – The remaining n The remaining n- -1 bits represent magnitude. 1 bits represent magnitude. – b n-1 b n-2 b 1 b 0 Sign Magnitude Dept. of CSE, IIT KGP

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend