3.1 Denary, Binary and Hexadecimal Number Systems 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) 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) A B C D E 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) A B C D E F Different representations of number values from 10 to 15 using denary, binary and hexadecimal number systems
3.2 Number System Conversion From a Binary Number or a Hexadecimal Number to a Denary Number 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. Binary to Denary Hexadecimal to Denary
3.2 Number System Conversion From a Binary Number or a Hexadecimal Number to a Denary Number Binary to Denary Only digits 0 and 1 are used in the binary number system. The binary number in its expanded form is: Therefore, = 1 × × × × 2 0 = Binary digit1011 Place value Digit value1 × × × × 2 0
3.2 Number System Conversion From a Binary Number or a Hexadecimal Number to a Denary Number Hexadecimal to Denary 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 2CA9 16 in its expanded form is: Therefore, 2CA9 16 = 2 × C × A × × 16 0 = Hexadecimal digit2CA9 Place value Digit value2 × 16 3 C × 16 2 A × × 16 0
3.2 Number System Conversion From a Denary Number to a Binary Number or a Hexadecimal Number 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 From a Binary Number to a Hexadecimal Number 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 From a Hexadecimal Number to a Binary Number 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: 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 From a Hexadecimal Number to a Binary Number 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: 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 Binary number system the only number system that a computer uses in storing numbers and performing arithmetic Bit, Byte and Word Bit A single binary digit The basic unit for storing data in a computer A single bit can only represent two distinct values. For example: 0 or 1, True or False, On or Off If more distinct values need to be represented, a series of bits can be used. For example: 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 Bit, Byte and Word Byte A data unit used in computers Consists of 8 bits and is the smallest addressable unit in a microprocessor. UnitAbbreviationRemark BitbThe basic unit of a binary digit ByteB1 B = 8 b KilobyteKB1 KB = 2 10 B = 1,024 bytes MegabyteMB1 MB = 2 20 B = 1,024 KB (or 1,048,576 bytes) GigabyteGB1 GB = 2 30 B = 1,024 MB (about 1 billion bytes) TerabyteTB1 TB = 2 40 B = 1,024 GB (about 1 trillion bytes) Measurement units of data
3.3 Number Representation in Computer Systems Bit, Byte and Word Word 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 Representation of Unsigned Binary Integer 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 Representation of Unsigned Binary Integer Word length Minimum unsigned binary integer allowed Maximum unsigned binary integer allowed 4-bit = = bit = = bit = = 65, bit = = 16,777, Minimum and maximum unsigned values of various word lengths
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Two common methods to represent signed binary integers: Sign-and-magnitude Two’s complement
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Sign-and-magnitude The leftmost bit, known as sign bit, is used to specify the sign of a binary integer. The sign bit: 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 Representation of Signed Binary Integer Sign-and-magnitude Word length Minimum signed binary integer allowed Maximum signed binary integer allowed 4-bit = – = bit = – = bit = –32, = 32, bit = –8,388, = 8,388, Minimum and maximum values represented by sign-and-magnitude integers in different word lengths
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Sign-and-magnitude The shortfall is that there are two representations of zero: Both and represent zero in an 8-bit signed binary integer. It is rarely used in computer systems.
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Two’s Complement Widely used in computers to represent binary integers All integers have unique representations. Two’s complement Decimal value Two’s complement Decimal value Two’s complement of a 4-bit integer
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Two’s Complement Representation of a positive binary integer is exactly the same as that of the original value To get a negative binary integer: 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 Representation of Signed Binary Integer Two’s Complement Word length Smallest negative binary integer allowed Largest positive binary integer allowed Two’s complementDenary valueTwo’s complementDenary value 4-bit – bit – bit –32, , Minimum and maximum two’s complement values and their corresponding denary values of various word lengths
3.3 Number Representation in Computer Systems Representation of Signed Binary Integer Two’s Complement Word length Smallest negative binary integer allowed Largest positive binary integer allowed Two’s complementDenary valueTwo’s complementDenary value 4-bit – bit – bit –32, , 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 Addition and Subtraction of Unsigned Integers 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 Addition and Subtraction of Unsigned Integers Overflow Error Occur when the calculation result is outside the range that the digits can represent. For example: _ Examples of overflow errors – _ Carry digit cannot be stored in the resulting bit pattern. Borrow occurs to the left of the leftmost bit.
3.4 Addition and Subtraction of Different Number Representations Addition and Subtraction of Two’s Complement Numbers 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 Addition and Subtraction of Two’s Complement Numbers 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 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 ASCII Two common character coding systems: American Standard Code for Information Interchange (ASCII) Extended Binary-Coded Decimal Interchange Code (EBCDIC) Each code represents either a printable character or a non- printable 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 ASCII DecBinaryCharacterDecBinaryCharacterDecBinaryCharacter SP < ! / = " > # ? % A & B ' C ( D ) E * F G , : H ; I ASCII code table
3.5 Character Coding Systems ASCII DecBinaryCharacterDecBinaryCharacterDecBinaryCharacter J X f K Y g L Z h M [ i N \ j O ] k P ^ l Q _ m R ` n S a o T b p U c q V d r W e s ASCII code table
3.5 Character Coding Systems ASCII In practice, the 7-bit ASCII code is often embedded in an 8-bit code, where the leftmost bit is set as ‘0’. DecBinaryCharacterDecBinaryCharacterDecBinaryCharacter t x | u y } v z ~ w { DEL ASCII code table
3.5 Character Coding Systems Chinese Character Coding Systems 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: Big5 code Guo Biao (GB) code Unicode
3.5 Character Coding Systems Chinese Character Coding Systems Big5 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 Chinese Character Coding Systems Hexadecimal representation Binary representation Character represented in Big5 code Character represented in GB code AA 昇狜 AA 服狝 AA 朋狟 AA 杭狢 Examples of Chinese characters represented in Big5 code and GB codes
3.5 Character Coding Systems Unicode 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 Unicode Hexadecimal representationBinary representationCharacter 4E3D 麗 4E3E 舉 4EAD 亭 4EAE 亮 Examples of Unicode
3.5 Character Coding Systems Unicode Hexadecimal representationBinary representationCharacter 4E3D 麗 4E3E 舉 4EAD 亭 4EAE 亮 Examples of Unicode