920 likes | 1.55k Views
Number Systems. Character Representation. ASCII American Standard Code for Information Interchange Standard encoding scheme used to represent characters in binary format on computers 7-bit encoding, so 128 characters can be represented
E N D
Character Representation • ASCII • American Standard Code for Information Interchange • Standard encoding scheme used to represent characters in binary format on computers • 7-bit encoding, so 128 characters can be represented • 0 to 31 (& 127) are "control characters" (cannot print)
Binary Data • Decimal, hex & character representations are easier for humans to understand; however… • All the data in the computer is binary • An int is typically 32 binary digits • int y = 5; (y = 0x00000005;) • In computer y = 00000000 00000000 00000000 00000101 • A char is typically 8 binary digits • char x = 5; (or char x = 0x05;) • In computer, x = 00000101
How Computers Store Numbers • Computer systems are constructed of digital electronics. That means that their electronic circuits can exist in only one of two states: on or off. • Most computer electronics use voltage levels to indicate their present state. For example, a transistor with five volts would be considered "on", while a transistor with no voltage would be considered "off.” • These patterns of "on" and "off" stored inside the computer are used to encode numbers using the binary number system. • Because of their digital nature, a computer's electronics can easily manipulate numbers stored in binary by treating 1 as "on" and 0 as "off.”
Number System • A number system defines how a number can be represented using distinct symbols. A number can be represented differently in different systems. For example, the two numbers (2A)16 and (52)8 both refer to the same quantity, (42)10, but their representations are different.
Common Number Systems S = {0, 1, 2, 3, 4, 5, 6, 7}
Decimal • The word decimal is derived from the Latin root decem (ten). In this system the base b = 10 and we use ten symbols: • The symbols in this system are often referred to as decimal digits or just digits. S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Binary • The word binary is derived from the Latin root bini (or two by two). In this system the base b = 2 and we use only two symbols: • The symbols in this system are often referred to as binary digits or bits (binary digit). S = {0, 1}
Hexadecimal • The word hexadecimal is derived from the Greek root hex (six) and the Latin root decem (ten). In this system the base b = 16 and we use sixteen symbols to represent a number. The set of symbols is: • Note that the symbols A, B, C, D, E, F are equivalent to 10, 11, 12, 13, 14, and 15 respectively. The symbols in this system are often referred to as hexadecimal digits. • Each hexadecimal digit represents four binary digits (bits), and the primary use of hexadecimal notation is a human-friendly representation of binary-coded values in computing and digital electronics. S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
Octal • The word octal is derived from the Latin root octo (eight). In this system the base b = 8 and we use eight symbols to represent a number. The set of symbols is: • Each octal digit represents three binary digits (bits) S = {0, 1, 2, 3, 4, 5, 6, 7}
Types of Number System S = {0, 1, 2, 3, 4, 5, 6, 7}
Conversion Among Bases The possibilities: Decimal Octal Binary Hexadecimal
Quick Example 2510=110012=318=1916 Base
Binary to Decimal Decimal Octal Binary Hexadecimal
Binary to Decimal Technique • Multiply each bit by 2n, where n is the “power” of the bit • The power is the position of the bit, starting from 0 on the right • ADD the results Example 1010112=> 1 x 20 = 1 1 x 21 = 2 0 x 22 = 0 1 x 23 = 8 0 x 24 = 0 1 x 25 = 32 4310 Bit 0
FRACTIONSBinary to Decimal Technique • Multiply each bit by 2n, where n is the “power” of the bit • The power is the position of the bit, starting from 0 on the right • ADD the results Example 21 20 2-1 2-2 2-3 2-4 10.01102 => 1 0 . 0 1 1 0 • 0 x (1/16) = 0 • 1 x (1/8) = 1/8 • 1 x (1/4) = 1/4 • 0 x (1/2) = 0 • 0 x (1) = 0 • 1 x (2) = 2 2-4 Bit 0 2-3 2-2 2-1 20 21 Ans: 2.375
Octal to Decimal Decimal Octal Binary Hexadecimal
Octal to Decimal Technique • Multiply each bit by 8n, where n is the “power” of the bit • The power is the position of the bit, starting from 0 on the right • Add the results Example 7248 => 4 x 80 = 4 2 x 81 = 16 7 x 82 = 448 46810 Bit 0
Hexadecimal to Decimal Decimal Octal Binary Hexadecimal
Hexadecimal to Decimal Technique • Multiply each bit by 16n, where n is the “power” of the bit • The power is the position of the bit, starting from 0 on the right • Add the results Example Bit 0 ABC16=> C x 160 = 12 x 1 = 12B x 161 = 11 x 16 = 176A x 162 = 10 x 256 = 2560 274810
Decimal to Binary Decimal Octal Binary Hexadecimal
Decimal to Binary Technique • Divide by the base 2, keep track of the remainder • Keep dividing until the quotient is 0. • Take the remainder from the bottom and move upwards as the answer E.g.: Convert 12510 to binary Take the remainder from bottom upwards as answer Ans: 11111012
FRACTIONSDecimal to Binary Technique • For the numbers after the point, multiply it by 2 • From the answer, take again the fraction part and multiply it by 2 again • Keep on multiplying the fraction by 2 until the fraction part is 0 E.g.: Convert 12.2510 to binary 12 . 25 12 / 2 = 6 6 / 2 = 3 3 / 2 = 1 1 / 2 = 0 R0 R0 R1 R1 0.25 X 2 = 0.50 0.50 X 2 = 1.00 01 1100 Ans: 1100.012
FRACTIONSDecimal to Binary .14579x 20.29158x 20.58316x 21.16632x 20.33264x 20.66528x 21.33056 etc. 3.14579 11.001001...
Octal to Binary Decimal Octal Binary Hexadecimal
Octal to Binary Technique • Convert each octal digit to a 3-bit equivalent binary representation • 1 octal digit = 3 binary digits E.g.: Convert 7058 to binary Start from the right ‘0’ bit 4 2 1 4 2 1 4 2 1 7 0 5 111 000 101 Ans: 111 000 1012
Binary to Octal Decimal Octal Binary Hexadecimal
Binary to Octal Technique • Divide the binary bits in group of 3’s, starting from the RIGHT • Add 0’s to the last group to make it 3 bits • Convert each grouped binary to their octal digits E.g.: 10110101112 Start from the right ‘0’ bit Divide the binary numbers into groups of 3’s. Add ‘0’ to the last group to make it 3 bits 001 011 010 111 4 2 1 4 2 1 4 2 1 4 2 1 001 011 010 111 1 3 2 7 Ans: 13278
Hexadecimal to Binary Decimal Octal Binary Hexadecimal
Hexadecimal to Binary Technique • Convert each hexadecimal digit to a 4-bit equivalent binary representation • 1 hexadecimal digit = 4 binary digits E.g.: Convert 3A816 to binary Start from the right ‘0’ bit 8 4 2 1 8 4 2 1 8 4 2 1 3 A (10) 5 0011 1010 0101 Ans: 0011 1010 01012
Binary to Hexadecimal Decimal Octal Binary Hexadecimal
Binary to Hexadecimal Technique • Divide the binary bits in group of 4’s, starting from the RIGHT • Add 0’s to the last group to make it 4 bits • Convert each grouped binary to their hexadecimal digits E.g.: 10101110112 Start from the right ‘0’ bit Divide the binary numbers into groups of 4’s. Add ‘0’ to the last group to make it 4 bits 00101011 1011 8 4 2 1 8 4 2 1 8 4 2 1 0010 1011 1011 2 11 (B) 11 (B) Ans: 2BB16
Decimal to Octal Decimal Octal Binary Hexadecimal
Decimal to Octal Technique • Divide by the base 8, keep track of the remainder • Keep dividing until the quotient is 0. • Take the remainder from the bottom and move upwards as the answer E.g.: Convert 123410 to octal Take the remainder from bottom upwards as answer Ans: 23228
Decimal to Hexadecimal Decimal Octal Binary Hexadecimal
Decimal to Hexadecimal Technique • Divide by the base 16, keep track of the remainder • Keep dividing until the quotient is 0. • Take the remainder from the bottom and move upwards as the answer E.g.: Convert 207910 to binary Take the remainder from bottom upwards as answer Ans: 81F16
Octal to Hexadecimal Decimal Octal Binary Hexadecimal
Octal to Hexadecimal Technique • First step: Convert the octal digits to their 3-bits binary • Second step: Combined the binary obtained • Third step: Divide the binary into groups of 4 (hexadecimal) starting from the RIGHT • Fourth step: Find the hexadecimal digit from the grouped binary E.g.: 10768 1 0 7 6 001 000 111 110 Divide the binary numbers into groups of 4’s. Add ‘0’ to the last group to make it 4 bits Start from the right ‘0’ bit 8 4 2 1 8 4 2 1 8 4 2 1 0010 / 0011 / 1110 2 3 14 (E) Ans: 23E16
Hexadecimal to Octal Decimal Octal Binary Hexadecimal
Hexadecimal to Octal Technique • First step: Convert the hexadecimal digits to their 4-bits binary • Second step: Combined the binary obtained • Third step: Divide the binary into groups of 3 (octal ) starting from the RIGHT • Fourth step: Find the octal digit from the grouped binary E.g.: C4516 C (12) 4 5 1100 0100 0101 Start from the right ‘0’ bit Divide the binary numbers into groups of 4’s. Add ‘0’ to the last group to make it 4 bits 4 2 1 4 2 1 4 2 1 4 2 1 110 / 0 01 / 00 0 / 101 6 1 0 5 Ans: 61058
Binary Addition • Binary addition between 2 1-bit values: “TWO”
Binary Addition Two n-bit values • Add individual bits • Propagate carries • E.g.: 1 1 10101 + 11001 101110 21 + 25 46
Binary Subtraction • Binary subtraction between 2 1-bit values:
Binary Subtraction In situations like A – B, whereby the binary number ‘B’ is larger than A, swap the number so that B is in front: B – A. Then negate the answer: B – A = -Y 10111 is larger than 1001. So swap so that the larger number is in front and the smaller number is after. Subtract the numbers and negate the answer by putting the ‘-’ sign in front. Borrow ‘1’ from the next binary digit. Hence the number will become 10 which is equals to ‘2’. E.g.: 1001 – 10111 = ? 1 1011 - 101 110 10111 - 1001 - 1110
SIGN BINARYRepresentation of positive and negative numbers in a computer storage One way to represent positive or negative numbers would be to make the left most bit a SIGN bit. Example: 00001111 By using that bit for sign, now the maximum positive number we can represent is for 7 bits only: i.e.: 01111111 = +127 instead of 255 before. What happens if we make the entire byte all 1s? 11111111= -127 Only 7-bits taken for calculation SIGN bit If the SIGN bit is 1, the number is NEGATIVE If the SIGN bit is 0, the number is POSITIVE For number -15, the 8-bits binary is 10001111 For number +15, the 8-bits binary is 00001111
1’s complement Example of 1's Complement is as follows: The 1's complement of a number is found by changing all 1's to 0's and all 0's to 1's. This is called as taking complement or 1's complement
2’s complement Example of 2's Complement is as follows. The 2's complement of binary number is obtained by adding 1 to the Least Significant Bit (LSB) of 1's complement of the number. 2's complement = 1's complement + 1
SIGN BINARY (ADDITION & SUBTRACTION) Subtracting/Adding binary numbers are done by first taking the NEGATIVE value and apply what is known as: 1’s complement: convert 1 to 0 and 0 to 1 2’s complement: adding 1 to the 1’s complement. There are two situations of sign binary subtraction/addition: Negative number larger = - ANS Positive number larger = + ANS Sign binaries can be added or subtracted. E.g.: -120 + 200 is also equal to 200 - 120 E.g.1 gives + answer due to the positive number is larger than the negative. E.g.2 gives - answer due to the negative number is larger than the positive. E.g.1: 200 – 120 = +80 E.g.2: 120 – 200 = -80 Both situations give different result.
SIGN BINARY (ADDITION & SUBTRACTION) E.g.1: 23510– 10210 = 133 E.g.2: 10010 – 6010 = 40 First find the 8-bits binary of each decimal. First find the 8-bits binary of each decimal. 11101011 01100110 = 235 = 102 01100100 00111100 = 100 = 60 • 1’s comp 10011001 + 1 • 1’s comp 11000011 + 1 Ignore the carry bcos there should only be 8-bits Ignore the carry bcos there should only be 8-bits • 2’s comp • 2’s comp 11000100 +01100100 = -60 = 100 10011010 +11101011 = -102 = 235 1 10000101 1 = 40 = 133 00101000