3 1 Denary Binary and Hexadecimal Number Systems
3. 1 Denary, Binary and Hexadecimal Number Systems n n n We use the denary (base 10) number system in our daily life for counting and calculation. Computers use the binary (base 2) number system for data representations and manipulations. Programmers use hexadecimal (base 16) number system to communicate with computers.
3. 1 Denary, Binary and Hexadecimal Number Systems Denary (base 10) Binary (base 2) Hexadecimal (base 16) 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 8 9 1001 9 Different representations of number values from 0 to 9 using denary, binary and hexadecimal number systems
3. 1 Denary, Binary and Hexadecimal Number Systems Denary (base 10) Binary (base 2) Hexadecimal (base 16) 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F Different representations of number values from 10 to 15 using denary, binary and hexadecimal number systems
3. 1 Denary, Binary and Hexadecimal Number Systems Denary (base 10) Binary (base 2) Hexadecimal (base 16) 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F Different representations of number values from 10 to 15 using denary, binary and hexadecimal number systems
3. 2 Number System Conversion n From a Binary Number or a Hexadecimal Number to a Denary Number n By evaluating the place values of the digits of a binary number or hexadecimal number, we can find the corresponding values in the denary number system. nary e Binary to D Hexadecimal to Denary
3. 2 Number System Conversion n From a Binary Number or a Hexadecimal Number to a Denary Number n Binary to Denary n n Only digits 0 and 1 are used in the binary number system. The binary number 10112 in its expanded form is: Binary digit 1 0 1 1 Place value 23 22 21 20 Digit value 1 × 23 0 × 22 1 × 21 1 × 20 Therefore, 10112 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 = 1110
3. 2 Number System Conversion n From a Binary Number or a Hexadecimal Number to a Denary Number n Hexadecimal to Denary n n n In the hexadecimal number system, the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F are used. A, B, C, D, E, F represent 10, 11, 12, 13, 14, 15 respectively. The hexadecimal number 2 CA 916 in its expanded form is: Hexadecimal digit 2 C A 9 Place value 163 162 161 160 Digit value 2 × 163 C × 162 A × 161 1 × 160 Therefore, 2 CA 916 = 2 × 163 + C × 162 + A × 161 + 9 × 160 = 1143310
3. 2 Number System Conversion n From a Denary Number to a Binary Number or a Hexadecimal Number n To convert a denary number to another number system with base b: 1. Divide the denary number by b repetitively until the quotient is smaller than b. 2. Obtain the answer by writing up from the quotient to the remainders in reverse order.
3. 2 Number System Conversion n From a Binary Number to a Hexadecimal Number n To convert a binary number into a hexadecimal number: 1. Group the digits of the binary number by four starting from the righthand side. 2. Replace each group of the four digits by an equivalent hexadecimal digit.
3. 2 Number System Conversion n From a Hexadecimal Number to a Binary Number n n To convert a hexadecimal number into a binary number: 1. Convert each digit of the hexadecimal number into a group of four binary digits. 2. Obtain the binary number by grouping all the binary digits together. The big problem with the binary system is verbosity. For example: n n A decimal value 202 requires 8 binary digits, making it difficult to read and remember. Hexadecimal numbers are much more compact than binary numbers and it is easy to convert from a hexadecimal number to a binary number and vice versa.
3. 2 Number System Conversion n From a Hexadecimal Number to a Binary Number n n To convert a hexadecimal number into a binary number: 1. Convert each digit of the hexadecimal number into a group of four binary digits. 2. Obtain the binary number by grouping all the binary digits together. The big problem with the binary system is verbosity. For example: n n A decimal value 202 requires 8 binary digits, making it difficult to read and remember. Hexadecimal numbers are much more compact than binary numbers and it is easy to convert from a hexadecimal number to a binary number and vice versa.
3. 3 Number Representation in Computer Systems n Binary number system n n the only number system that a computer uses in storing numbers and performing arithmetic Bit, Byte and Word n Bit n n n A single binary digit The basic unit for storing data in a computer A single bit can only represent two distinct values. For example: n n If more distinct values need to be represented, a series of bits can be used. For example: n n 0 or 1, True or False, On or Off A 8 -bit binary number can hold 256 distinct values. An n-bit binary number can represent 2 n distinct values.
3. 3 Number Representation in Computer Systems n Bit, Byte and Word n Byte n n Unit A data unit used in computers Consists of 8 bits and is the smallest addressable unit in a microprocessor. Abbreviation Remark Bit b The basic unit of a binary digit Byte B 1 B=8 b Kilobyte KB 1 KB = 210 B = 1, 024 bytes Megabyte MB 1 MB = 220 B = 1, 024 KB (or 1, 048, 576 bytes) Gigabyte GB 1 GB = 230 B = 1, 024 MB (about 1 billion bytes) Terabyte TB 1 TB = 240 B = 1, 024 GB (about 1 trillion bytes) Measurement units of data
3. 3 Number Representation in Computer Systems n Bit, Byte and Word n n n n Modern computers can handle data stored in two or more bytes at a time. The natural unit of data used by a computer is called a word. The word length (word size) is measured in bits. Modern computers usually have a word length of 16, 32 or 64 bits.
3. 3 Number Representation in Computer Systems n Representation of Unsigned Binary Integer n n n Unsigned integer is a type of integer representation that contains no sign symbol. Non-negative integers such as population and sales volume of books can be represented by unsigned integers. All bits in a word are used to represent the magnitude.
3. 3 Number Representation in Computer Systems n Representation of Unsigned Binary Integer Word length Minimum unsigned binary integer allowed Maximum unsigned binary integer allowed 4 -bit 00002 = 010 11112 = 1510 8 -bit 00002 = 010 11112 = 25510 16 -bit 00002 = 010 11112 = 65, 53510 24 -bit 0000 00002 = 010 1111 11112 = 16, 777, 21510 Minimum and maximum unsigned values of various word lengths
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Two common methods to represent signed binary integers: ude agnit m d n a n g Si Two’s c omplem ent
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Sign-and-magnitude n n The leftmost bit, known as sign bit, is used to specify the sign of a binary integer. The sign bit: n n n 0: a positive integer 1: a negative integer The rest of the bits are used to store the magnitude of the binary number.
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Sign-and-magnitude Word length Minimum signed binary integer allowed Maximum signed binary integer allowed 4 -bit 11112 = – 710 01112 = 710 8 -bit 11112 = – 12710 0111 11112 = 12710 16 -bit 11112 = – 32, 76710 0111 11112 = 32, 76710 24 -bit 1111 11112 = – 8, 388, 60710 0111 1111 11112 = 8, 388, 60710 Minimum and maximum values represented by sign-and-magnitude integers in different word lengths
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Sign-and-magnitude n The shortfall is that there are two representations of zero: n n Both 1000 00002 and 00002 represent zero in an 8 -bit signed binary integer. It is rarely used in computer systems.
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Two’s Complement n n Widely used in computers to represent binary integers All integers have unique representations. Two’s complement 0111 0110 0101 0100 0011 0010 0001 0000 Decimal value 7 6 5 4 3 2 1 0 Two’s complement 1111 1110 1101 1100 1011 1010 1001 1000 Decimal value -1 -2 -3 -4 -5 -6 -7 -8 Two’s complement of a 4 -bit integer
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Two’s Complement n n Representation of a positive binary integer is exactly the same as that of the original value To get a negative binary integer: n n n Starting from the corresponding positive binary integer, invert all bits from ‘ 0’ to ‘ 1’ and ‘ 1’ to ‘ 0’. The result is known as one’s complement. Add 1 to the one’s complement. The above steps can also be used to convert a negative binary integer into a positive binary integer.
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Word length Two’s Complement Smallest negative binary integer allowed Largest positive binary integer allowed Two’s complement Denary value 4 -bit 10002 – 810 01112 710 8 -bit 1000 00002 – 12810 0111 11112 12710 16 -bit 1000 00002 – 32, 76810 0111 11112 32, 76710 Minimum and maximum two’s complement values and their corresponding denary values of various word lengths
3. 3 Number Representation in Computer Systems n Representation of Signed Binary Integer n Word length Two’s Complement Smallest negative binary integer allowed Largest positive binary integer allowed Two’s complement Denary value 4 -bit 10002 – 810 01112 710 8 -bit 1000 00002 – 12810 0111 11112 12710 16 -bit 1000 00002 – 32, 76810 0111 11112 32, 76710 Minimum and maximum two’s complement values and their corresponding denary values of various word lengths
3. 4 Addition and Subtraction of Different Number Representations n Addition and Subtraction of Unsigned Integers n n n The calculation on both addition and subtraction of unsigned integers in other number systems is the same as that in the denary system. A ‘carry’ is generated when the sum of digits equals or exceeds the base value. A ‘borrow’ from the left digit is necessary if a larger digit is subtracted from a smaller one.
3. 4 Addition and Subtraction of Different Number Representations n Addition and Subtraction of Unsigned Integers n Overflow Error n Occur when the calculation result is outside the range that the digits can represent. For example: 1111 0000 +_ 0001 1100 1 0000 1100 Carry digit cannot be stored in the resulting bit pattern. 1 0000 1000 – _ 0001 1000 1111 0000 Borrow occurs to the left of the leftmost bit. Examples of overflow errors
3. 4 Addition and Subtraction of Different Number Representations n Addition and Subtraction of Two’s Complement Numbers n n No matter what the sign bits are, the addition of two’s complement numbers are exactly the same as the addition of two unsigned binary integers. Overflow error occurs when two numbers of the same sign are added and the result has the opposite sign.
3. 4 Addition and Subtraction of Different Number Representations n Addition and Subtraction of Two’s Complement Numbers n n No matter what the sign bits are, the addition of two’s complement numbers are exactly the same as the addition of two unsigned binary integers. Overflow error occurs when two numbers of the same sign are added and the result has the opposite sign.
3. 5 Character Coding Systems n n Other than numeric data, a computer needs to store and process characters. The character coding system is a way to represent characters in a form that can be manipulated efficiently in a computer.
3. 5 Character Coding Systems n ASCII n Two common character coding systems: n n n American Standard Code for Information Interchange (ASCII) Extended Binary-Coded Decimal Interchange Code (EBCDIC) Each code represents either a printable character or a nonprintable character (e. g. end-of-line character) Each ASCII character consists of 7 bits, and it can contain 2 7 (i. e. 128) different characters. They include all the alphanumeric characters (the small and capital English letters, digits from 0 to 9) and graphics symbols.
3. 5 Character Coding Systems n ASCII Dec Binary Character 032 0100000 SP 046 0101110 . 060 0111100 < 033 0100001 ! 047 0101111 / 061 0111101 = 034 0100010 " 048 0110000 0 062 0111110 > 035 0100011 # 049 0110001 1 063 0111111 ? 036 0100100 $ 050 0110010 2 064 1000000 @ 037 0100101 % 051 0110011 3 065 1000001 A 038 0100110 & 052 0110100 4 066 1000010 B 039 0100111 ' 053 0110101 5 067 1000011 C 040 0101000 ( 054 0110110 6 068 1000100 D 041 0101001 ) 055 0110111 7 069 1000101 E 042 0101010 * 056 0111000 8 070 1000110 F 043 0101011 + 057 0111001 9 071 1000111 G 044 0101100 , 058 0111010 : 072 1001000 H 045 0101101 - 059 0111011 ; 073 1001001 I ASCII code table
3. 5 Character Coding Systems n ASCII Dec Binary Character 074 1001010 J 088 1011000 X 102 1100110 f 075 1001011 K 089 1011001 Y 103 1100111 g 076 1001100 L 090 1011010 Z 104 1101000 h 077 1001101 M 091 1011011 [ 105 1101001 i 078 1001110 N 092 1011100 106 1101010 j 079 1001111 O 093 1011101 ] 107 1101011 k 080 1010000 P 094 1011110 ^ 108 1101100 l 081 1010001 Q 095 1011111 _ 109 1101101 m 082 1010010 R 096 1100000 ` 1101110 n 083 1010011 S 097 1100001 a 111 1101111 o 084 1010100 T 098 1100010 b 112 1110000 p 085 1010101 U 099 1100011 c 113 1110001 q 086 1010110 V 100 1100100 d 114 1110010 r 087 1010111 W 101 1100101 e 115 1110011 s ASCII code table
3. 5 Character Coding Systems n ASCII Dec Binary Character 116 1110100 t 120 1111000 x 124 1111100 | 117 1110101 u 121 1111001 y 125 1111101 } 118 1110110 v 122 1111010 z 126 1111110 ~ 119 1110111 w 123 1111011 { 127 1111111 DEL ASCII code table n In practice, the 7 -bit ASCII code is often embedded in an 8 -bit code, where the leftmost bit is set as ‘ 0’.
3. 5 Character Coding Systems n Chinese Character Coding Systems n n The standard ASCII: 128 characters The extended ASCII: 256 characters There are more than 100, 000 Chinese characters and we use more than 10, 000 of them in our daily lives. Chinese characters are usually represented in: n n n Big 5 code Guo Biao (GB) code Unicode
3. 5 Character Coding Systems n Chinese Character Coding Systems n n Big 5 code: traditional Chinese GB code: simplified Chinese Both character coding systems use two bytes to represent one Chinese character. If a Chinese text file is processed with the wrong coding system, strange and meaningless characters will appear.
3. 5 Character Coding Systems n Chinese Character Coding Systems Hexadecimal representation Binary representation Character represented in Big 5 code Character represented in GB code AA 40 1010 0100 0000 昇 狜 AA 41 1010 0100 0001 服 狝 AA 42 1010 0100 0010 朋 狟 AA 43 1010 0100 0011 杭 狢 Examples of Chinese characters represented in Big 5 code and GB codes
3. 5 Character Coding Systems n Unicode n n n Used to represent all the existing languages in the world Contain about 100, 000 characters The representations of Unicode known as code points are variable in length. Made up of one to four bytes The code points are carefully defined so that the most frequently used characters, such as the ASCII codes, are put at the front of the code table. These characters can be represented by a one-byte code point.
3. 5 Character Coding Systems n Unicode Hexadecimal representation Binary representation Character 4 E 3 D 0100 1110 0011 1101 麗 4 E 3 E 0100 1110 0011 1110 舉 4 EAD 0100 1110 1010 1101 亭 4 EAE 0100 1110 1010 1110 亮 Examples of Unicode
3. 5 Character Coding Systems n Unicode Hexadecimal representation Binary representation Character 4 E 3 D 0100 1110 0011 1101 麗 4 E 3 E 0100 1110 0011 1110 舉 4 EAD 0100 1110 1010 1101 亭 4 EAE 0100 1110 1010 1110 亮 Examples of Unicode
- Slides: 39