Debian Codegraph @

doc/1-intro/hex.md

raw
1 # hexadecimal
2
3 the hexadecimal numeral system differs from the decimal system by having 16 digits rather than 10:
4
5 hex | dec
6 :--:|---:
7 `0` | 0
8 `1` | 1
9 `2` | 2
10 `3` | 3
11 `4` | 4
12 `5` | 5
13 `6` | 6
14 `7` | 7
15 `8` | 8
16 `9` | 9
17 `A` | 10
18 `B` | 11
19 `C` | 12
20 `D` | 13
21 `E` | 14
22 `F` | 15
23
24 when there is more than one digit, these are multiplied by 16, 256, 4096 and so on rather than 10, 100, 1000:
25
26 hex | dec
27 :---:|---:
28 `00` | 0
29 `04` | 4
30 `08` | 8
31 `0F` | 15
32 `10` | 16
33 `11` | 17
34 `12` | 18
35 `13` | 19
36 `20` | 32
37 `30` | 48
38 `40` | 64
39
40 ## hex to decimal
41
42 for example, take hexadecimal number `AA`:
43
44 ```
45 2nd digit -\ /- 1st digit
46 A A
47 16^1*10 = 16*10 = 160 + 10 = 170
48 ```
49
50 now for hexadecimal number `4C5F`:
51
52 ```
53
54 3rd digit -\ /- 2nd digit
55 4th digit -\ | | /- 1st digit
56 4 C 5 F
57 | | | |
58 | | | 15 = 15 = 15 +
59 | | \16^1*5 = 16 * 5 = 80
60 | \--- 16^2*12 = 256 * 12 = 3072
61 \--------- 16^3*4 = 4096 * 4 = 16384
62 -------
63 = 19551
64 ```
65
66 ## decimal to hex
67
68 if it's less than 16, just memorize the table at the top of this document.
69
70 otherwise find the power of 16 that is closest to the number you want to convert, but no larger than the number.
71 then divide, and take the remainder.
72 divide the remainder with the previous power of 16, until the divider is 1.
73
74 for example, for the decimal number `220`:
75
76 ```
77 220 ÷ 16 = 13 (r = 12) D
78 12 ÷ 1 = 12 (stop here) C
79
80 = DC
81 ```
82
83 now for decimal number `69420`:
84
85 ```
86 69420 ÷ 65536 = 1 (r = 3884) 1
87 3884 ÷ 4096 = 0 (r = 3884) 0
88 3884 ÷ 256 = 15 (r = 44) F
89 44 ÷ 16 = 2 (r = 12) 2
90 12 ÷ 1 = 12 (stop here) C
91
92 = 10F2C
93 ```
94
95 ## bitmask
96
97 a bitmask is a value that actually represents a set of individual binary bits to be toggled, some of which may be grouped to form small binary numbers. these are used by adding up the value of each bit and converting the result to hex. in macros, these are shown in decimal.
98
99 bit | binary | decimal
100 :----:|:-----------:|--------:
101 bit 7 | `1000 0000` | 128
102 bit 6 | `0100 0000` | 64
103 bit 5 | `0010 0000` | 32
104 bit 4 | `0001 0000` | 16
105 bit 3 | `0000 1000` | 8
106 bit 2 | `0000 0100` | 4
107 bit 1 | `0000 0010` | 2
108 bit 0 | `0000 0001` | 1
109
110 for example, to turn bits 7 and 5 on, and put `110` (decimal 6) in bits 1 to 3:
111
112 bit | binary | decimal
113 :----:|:-----------:|--------:
114 bit 7 | `1... ....` | 128
115 bit 6 | `.0.. ....` | 0
116 bit 5 | `..1. ....` | 32
117 bit 4 | `...0 ....` | 0
118 bit 3 | `.... 1...` | 8
119 bit 2 | `.... .1..` | 4
120 bit 1 | `.... ..0.` | 0
121 bit 0 | `.... ...0` | 0
122
123 added together, the resulting binary is `1010 1100`, which converts to hex as `AC` and to decimal as 172.
124
125 ## hex to decimal table
126
127 hex | `0` | `1` | `2` | `3` | `4` | `5` | `6` | `7` | `8` | `9` | `A` | `B` | `C` | `D` | `E` | `F`
128 -----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:|----:
129 `00` | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
130 `10` | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31
131 `20` | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47
132 `30` | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63
133 `40` | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79
134 `50` | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95
135 `60` | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111
136 `70` | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127
137 `80` | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143
138 `90` | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159
139 `A0` | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175
140 `B0` | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191
141 `C0` | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207
142 `D0` | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223
143 `E0` | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239
144 `F0` | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255