Binary and Hexadecimal Number Systems Dr Koushik Chakraborty
Binary and Hexadecimal Number Systems Dr. Koushik Chakraborty Utah State University
Decimal (Base 10) Numbers • We are familiar with these – we’ve seen them our entire lives! • Examples: • 1, 234 • 1, 000 + 200 + 30 + 4 • (1 * 103) + (2 * 102) + (3 * 101) + (4 * 100) • 56. 789 • (5 * 101) + (6 * 100) + (7 * 10 -1) + (8 * 10 -2) + (9 * 10 -3) • We call it “Base 10” because each position has a value that is associated with a power of 10. • Notice that all digits must be less than 10 (i. e. 0 through 9)
Decimal (Base 10) Numbers • Expand this number: 219, 043. 04
Decimal (Base 10) Numbers • Expand this number: 219, 043. 04 • (2 * 105) + (1 * 104) + (9 * 103) + (4 * 101) + (3 * 100) + (4 * 10 -2)
Decimal (Base 10) Numbers • Expand this number: 219, 043. 04 • (2 * 105) + (1 * 104) + (9 * 103) + (4 * 101) + (3 * 100) + (4 * 10 -2) • Compress this number: (3 * 106) + (9 * 104) + (7 * 101) + (1 * 10 -4)
Decimal (Base 10) Numbers • Expand this number: 219, 043. 04 • (2 * 105) + (1 * 104) + (9 * 103) + (4 * 101) + (3 * 100) + (4 * 10 -2) • Compress this number: (3 * 106) + (9 * 104) + (7 * 101) + (1 * 10 -4) • 3, 090, 070. 0001
Binary (Base 2) Numbers • “There are 10 kinds of people in the world: those who understand binary and those who don’t. ” • Computers “speak” binary (on/off) • Examples: • Binary 1001 • (1 * 23) + (1 * 20) = 9 decimal • 101. 011 • (1 * 22) + (1 * 20) + (1 * 2 -2) + (1 * 2 -3) • We call it “Base 2” because each position has a value that is associated with a power of 2. • Notice that all digits must be less than 2 (i. e. 0 through 1)
Binary (Base 2) Numbers • Convert the following binary numbers into decimal: • 11011 • 1000000 • 111
Binary (Base 2) Numbers • Convert the following binary numbers into decimal: • 11011 • 27 decimal • 1000000 • 64 decimal • 111 • 7. 875 decimal
Binary (Base 2) Numbers • Convert the following decimal numbers into binary: • 31 • 23 • 17. 25
Binary (Base 2) Numbers • Convert the following decimal numbers into binary: • 31 • 11111 • 23 • 10111 • 17. 25 • 10001. 01
Adding Binary Numbers • Same carry rules apply as in decimal • Remember, each digit can only be a 0 or a 1 • Example 10010101 + 1100 ----10100001
Adding Binary Numbers • Same carry rules apply as in decimal • Remember, each digit can only be a 0 or a 1 • Add these binary numbers 10101 + 110 ------ 1101 + 101 ----- 1111 + 11 -----
Adding Binary Numbers • Same carry rules apply as in decimal • Remember, each digit can only be a 0 or a 1 • Add these binary numbers 10101 + 110 -----11011 1101 + 101 ----10010 1111 + 11 ----10010
Subtracting Binary Numbers • Same borrow rules apply as in decimal • Remember, each digit can only be a 1 or a 0 • Example 10010101 1100 -------10001001
Subtracting Binary Numbers • Same borrow rules apply as in decimal • Remember, each digit can only be a 0 or a 1 • Subtract these binary numbers 10101 - 110 ------ 1101 ----- 1111 -----
Subtracting Binary Numbers • Same borrow rules apply as in decimal • Remember, each digit can only be a 0 or a 1 • Subtract these binary numbers 10101 - 110 -----1111 1101 ----1000 1111 ----1100
Hexadecimal (Base 16) Numbers • Hexadecimal is convenient as short-hand for binary • One hexadecimal digit expands to four binary digits • Letters A through F are used to represent values 10 through 15 as a single digit • Examples: • Hex 2 A 4 • (2 * 162) + (10 * 161) + (4 * 160) = 676 decimal • 2 E. 1 • (2 * 161) + (14 * 160) + (1 * 16 -1) = 46. 0625 • We call it “Base 16” because each position has a value that is associated with a power of 16. • Notice that all digits must be less than 16 (i. e. 0 through F)
Hexadecimal (Base 16) Numbers • Convert the following hexadecimal numbers into decimal: • 0 x 20 • 0 x. FF • 0 x. C. 8
Hexadecimal (Base 16) Numbers • Convert the following hexadecimal numbers into decimal: • 0 x 20 • 32 decimal • 0 x. FF • 255 decimal • 0 x. C. 8 • 12. 5 decimal
Hexadecimal (Base 16) Numbers • Convert the following hexadecimal numbers into binary (each hex digit expands to 4 binary digits): • 20 • FF • C. 8
Hexadecimal (Base 16) Numbers • Convert the following hexadecimal numbers into binary (each hex digit expands to 4 binary digits): • 20 • 00100000 binary • FF • 1111 binary • C. 8 • 1100. 1000 binary
Adding Hex Numbers • Same carry rules apply as in decimal • Remember, each digit can be between 0 and F • Example 3 E +12 --50
Adding Hex Numbers • Same carry rules apply as in decimal • Remember, each digit can be between 0 and F • Add these hex numbers 281 + 9 E ---31 F FED + BE ---10 AB
Subtracting Hex Numbers • Same borrow rules apply as in decimal • Remember, each digit can be between 0 and F • Example 3 E -12 --2 C
Subtracting Hex Numbers • Same borrow rules apply as in decimal • Remember, each digit can be between 0 and F • Subtract these hex numbers 123 - EE ---- F 0 -77 ---
Subtracting Hex Numbers • Same borrow rules apply as in decimal • Remember, each digit can be between 0 and F • Subtract these hex numbers 123 - EE ---35 F 0 -77 --79
Negative Integers in Computers • Computers speak binary (1’s and 0’s) – how to represent negative numbers? • Two’s complement notation for signed integers • Within a specified container size, the most significant bit is the sign bit • 1 means negative • 0 means non-negative • The magnitude of a negative number is obtained by flipping all bits then adding 1
Negative Integers in Computers • Two’s complement examples • 11110000 • MSb is ‘ 1’, indicating a negative number • Magnitude is • Flip all bits: 11110000 -> 00001111 • Add 1 to result: 00001111 + 1 = 00010000 = -16 decimal • 1111 • MSb is ‘ 1’, indicating a negative number • Magnitude is • Flip all bits: 1111 -> 0000 • Add 1 to result: 0000 + 1 = 00000001 = -1 decimal
Negative Integers in Computers • Two’s complement examples • Convert -23 decimal to an 8 -bit two’s complement signed binary • 23 decimal is 00010111 binary • Flip all bits: 00010111 -> 11101000 • Add 1 to result -> 11101001
Negative Integers in Computers • Two’s complement also works in hexadecimal (obviously!) • 11110000 binary is 0 x. F 0 • MSb is ‘ 1’, indicating a negative number • Magnitude is • Flip all bits: 0 x. F 0 -> 0 x 0 F • Add 1 to result: 0 x 0 F + 0 x 01 = 0 x 10 = -16 decimal • 1111 binary is 0 x. FF • MSb is ‘ 1’, indicating a negative number • Magnitude is • Flip all bits: 0 x. FF -> 0 x 00 • Add 1 to result: 0 x 00 + 1 = 0 x 01 = -1 decimal
Negative Integers in Computers • Two’s complement signed number can be added/subtracted just like unsigned numbers: 11110000 is either 240 or -16 decimal +00001111 is always 15 decimal ----1111 is either 255 or -1 decimal
- Slides: 32