Base THE HEXADECIMAL BASE Assembly language programming By xorpd xorpd.net Objectives Introduction to base 16 You will understand the special relation between base 16 and base You will learn about the importance of base 16 You will recognize when would be a good time to use base 16 Base 16 Base 16 is called the Hexadecimal base, or just hex Base 16 contains 16 symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Examples: 1016 = ⋅ 16 + ⋅ 16 = 1610 1 2𝐴16 = 𝐴 ⋅ 16 + ⋅ 16 = 10 ⋅ 16 + ⋅ 16 = 4210 Usually marked with a "0𝑥" (Zero and x) prefix in high level programming languages 0𝑥10,0𝑥2𝑎 Hex to Binary How could we convert a hexadecimal number to a binary number? We could use the Remainder Evaluation method Example: Convert 𝐴𝐵1𝐶16 to base AB1C AB1 AB A 558E 558 55 2AC7 2AC 2A 1563 156 15 1 Hex to Binary How could we convert a hexadecimal number to a binary number? We could use the Remainder Evaluation method Example: Convert 𝐴𝐵1𝐶16 to base AB1C AB1 AB A 558E 558 55 2AC7 2AC 2A 1563 156 15 1 Hex to Binary How could we convert a hexadecimal number to a binary number? We could use the Remainder Evaluation method Example: Convert 𝐴𝐵1𝐶16 to base AB1C AB1 AB A 558E 558 55 2AC7 2AC 2A 1563 156 15 1 Hex to Binary How could we convert a hexadecimal number to a binary number? We could use the Remainder Evaluation method Example: Convert 𝐴𝐵1𝐶16 to base AB1C AB1 AB A 558E 558 55 2AC7 2AC 2A 1563 156 15 1 𝑪𝟏𝟔 = 𝟏𝟏𝟎𝟎𝟐 𝟏𝟏𝟔 = 𝟎𝟎𝟎𝟏𝟐 𝑩𝟏𝟔 = 𝟏𝟎𝟏𝟏𝟐 𝑨𝟏𝟔 = 𝟏𝟎𝟏𝟎𝟐 Hex to Binary How could we convert a hexadecimal number to a binary number? We could use the Remainder Evaluation method Example: Convert 𝐴𝐵1𝐶16 to base 𝑪𝟏𝟔 = 𝟏𝟏𝟎𝟎𝟐 𝟏𝟏𝟔 = 𝟎𝟎𝟎𝟏𝟐 𝑩𝟏𝟔 = 𝟏𝟎𝟏𝟏𝟐 𝑨𝟏𝟔 = 𝟏𝟎𝟏𝟎𝟐 𝐴𝐵1𝐶16 = 1010 1011 0001 1100 Every Hex digit is represented by exactly bits Hex to Binary (Cont.) Base 16 and Base “Get along”, because 24 = 16 There is a bonus lesson that explains this more rigorously In order to convert hexadecimal number to a binary number, it is enough to convert every hex digit into bits Example: 0𝑥𝐴𝐷5𝐷 =? 0𝑥𝐴 = 1010 = 10102 0𝑥𝐷 = 1310 = 11012 0𝑥5 = 510 = 01012 So 0𝑥𝐴𝐷5𝐷 = 1010 1101 0101 1101 Binary to Hex Example: Convert the number 1011010112 to hexadecimal base We first divide the bits of the number into groups of (beginning from the right): 0110 1011 The last set of bits might be of size less than We could imagine that there are leading zeroes: 0001 0110 1011 We convert every quadruple of bits into one hex digit: 00012 = 0𝑥1 01102 = 0𝑥6 10112 = 1110 = 0𝑥𝐵 We get that 1011010112 = 0𝑥16𝐵 Bases that get along The same phenomenon of easy conversion happens for every two bases 𝑏, 𝑐 Where 𝑏 = 𝑐 𝑑 for some 𝑑 In base 4, every digit is represented by exactly bits In base 8, every digit is represented by exactly bits Example: Convert the number 13178 to binary 18 = 0012 38 = 0112 78 = 1112 So 13178 = 001 011 001 111 Why would I care about hex In order to talk to the processor, you have to understand binary Binary numbers could sometimes be too long Many computers today work with groups of 32 bits or 64 bits Hex numbers have more symbols, thus much shorter than their Binary equivalent Which one is easier to read? 110111101010110110111110111011112 0𝑥𝐷𝐸𝐴𝐷𝐵𝐸𝐸𝐹 Conversion between Hex and Binary is immediate Conversion is done digit wise (unlike the general method of conversion between bases) Exercises Basic conversion exercises Use a pen and paper to solve Use a calculator / Computer to check your answers Interesting divisibility rules in base 16 Some more… Hex to Binary (Explained) Question: Why is every hex digit represented by binary digits? Representation in base 16: … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑎0 ⋅ 160 + 𝑎1 ⋅ 161 + 𝑎2 ⋅ 162 + 𝑎3 ⋅ 163 + ⋯ Hex to Binary (Explained) Representation in base 16: … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑎0 ⋅ 160 + 𝑎1 ⋅ 161 + 𝑎2 ⋅ 162 + 𝑎3 ⋅ 163 + ⋯ We could represent each hex digit using bits: 𝑎0 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑎1 = 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑎2 = 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 … Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 𝑎0 ⋅ 160 + 𝑎1 ⋅ 161 + 𝑎2 ⋅ 162 + 𝑎3 ⋅ 163 + … 16 = 𝑎0 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑎1 = 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑎2 = 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑎3 = 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 … 2 2 Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 𝑎0 ⋅ 160 + 𝑎1 ⋅ 161 + 𝑎2 ⋅ 162 + 𝑎3 ⋅ 163 + … … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 … 16 = 𝑎0 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑎1 = 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑎2 = 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑎3 = 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 … 2 2 ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + 2 2 Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 𝑎0 ⋅ 160 + 𝑎1 ⋅ 161 + 𝑎2 ⋅ 162 + 𝑎3 ⋅ 163 + … … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 … 16 = 𝑎0 = 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 𝑎1 = 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑎2 = 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑎3 = 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 … 2 2 ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + 2 2 … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + We will use the fact that 24 = 16 ! Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … ⋅ 20 + ⋅ 24 + ⋅ 28 + ⋅ 212 + Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 𝑏0,1 20 + 𝑏1,1 ⋅ 21 + 𝑏2,1 22 + 𝑏3,1 23 𝑏0,2 20 + 𝑏1,2 ⋅ 21 + 𝑏2,2 22 + 𝑏3,2 23 𝑏0,3 20 + 𝑏1,3 ⋅ 21 + 𝑏2,3 22 + 𝑏3,3 23 … ⋅ 160 + ⋅ 161 + ⋅ 162 + ⋅ 163 + ⋅2 + ⋅ 24 + ⋅ 28 + ⋅ 212 + … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 + 𝑏0,1 24 + 𝑏1,1 ⋅ 25 + 𝑏2,1 26 + 𝑏3,1 27 + 𝑏0,2 28 + 𝑏1,2 ⋅ 29 + 𝑏2,2 210 + 𝑏3,2 211 + 𝑏0,3 212 + 𝑏1,3 ⋅ 213 + 𝑏2,3 214 + 𝑏3,3 215 + … Hex to Binary (Explained) … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = 𝑏0,0 20 + 𝑏1,0 ⋅ 21 + 𝑏2,0 22 + 𝑏3,0 23 + 𝑏0,1 24 + 𝑏1,1 ⋅ 25 + 𝑏2,1 26 + 𝑏3,1 27 + 𝑏0,2 28 + 𝑏1,2 ⋅ 29 + 𝑏2,2 210 + 𝑏3,2 211 + 𝑏0,3 212 + 𝑏1,3 ⋅ 213 + 𝑏2,3 214 + 𝑏3,3 215 + … … 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 16 = … 𝑏3,3 𝑏2,3 𝑏1,3 𝑏0,3 𝑏3,2 𝑏2,2 𝑏1,2 𝑏0,2 𝑏3,1 𝑏2,1 𝑏1,1 𝑏0,1 𝑏3,0 𝑏2,0 𝑏1,0 𝑏0,0 Every hex digit is represented by exactly bits ... You will recognize when would be a good time to use base 16 Base 16 Base 16 is called the Hexadecimal base, or just hex Base 16 contains 16 symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F ... x) prefix in high level programming languages 0