Update
This commit is contained in:
parent
362a9efe1f
commit
8ac0ebb55b
10 changed files with 2041 additions and 2014 deletions
|
@ -1,10 +1,10 @@
|
|||
# Hexadecimal
|
||||
|
||||
Hexadecimal (also just *hex*) is a base-16 numeral system, very commonly used in [programming](programming.md) (alongside [binary](binary.md) and [octal](octal.md)). It basically works exactly the same as our traditional base 10 numeral system, but in addition to digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 adds also digits *A* (10), *B* (11), *C* (12), *D* (13), *E* (14) and *F* (15). In other words hexadecimal is nothing more than a different way of writing numbers -- for example instead of writing 123 in decimal we can write 7B in hexadecimal (to prevent confusion programmers often prefix hexadecimal numbers with `0x`, `#` and similar symbols, because sometimes a hexadecimal number may be formed with only digits 0 - 9 and could be confused with decimal number). Why is hexadecimal so special? Why 16? Why not just use normal decimal numbers? Well, this is out of convenience -- 16 is not an arbitrary number, it is a power of 2 (2^4 = 16); now since [digital](digital.md) [computers](computer.md) typically work with [bits](bit.md), i.e. 1s and 0s, groups of bits form binary numbers and these are (unlike decimal numbers) very easily converted to and from hexadecimal (exactly because the base 16 is a power of two base): it turns out that 4 bits (i.e. a group of 4 "1s and 0s") always convert exactly to one hexadecimal digit and vice versa, which is very nice and simplifies mental calculations. It also formats numbers nicely -- 8 bits will always be exactly 2 hexadecimal digits etc. That's basically all.
|
||||
Hexadecimal (also just *hex*) is a base-16 numeral system, very commonly used in [programming](programming.md) (alongside [binary](binary.md) and [octal](octal.md)). It more or less works exactly the same as our traditional base 10 numeral system, but in addition to digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 also includes digits *A* (10), *B* (11), *C* (12), *D* (13), *E* (14) and *F* (15). In other words hexadecimal is nothing more than a different way of writing numbers down -- for example instead of 123 in decimal we can write 7B in hexadecimal (to prevent confusion programmers frequently prefix hexadecimal numbers with `0x`, `#` and similar symbols, because oftentimes a hexadecimal number may only contain digits 0 - 9 and could be confused with a decimal number). Why is hexadecimal so special? Why 16? Why not just use normal decimal numbers? Well, this is out of convenience -- 16 is not an arbitrary number, it is a power of 2 (2^4 = 16); now since [digital](digital.md) [computers](computer.md) typically work with [bits](bit.md), i.e. 1s and 0s, groups of bits form binary numbers and these are (unlike decimal numbers) very easily converted to and from hexadecimal (exactly because the base 16 is a power of two base): it turns out that 4 bits (i.e. a group of 4 "1s and 0s") always convert exactly to one hexadecimal digit and vice versa, which is very nice and simplifies mental calculations. It also formats numbers nicely -- 8 bits will always be exactly 2 hexadecimal digits etc. That's basically all.
|
||||
|
||||
Hexadecimal is so common in programming that programmers often use the term "hex" or "hexadecimal" data to just mean "binary" data, e.g. as in "[hex editor](hex_editor.md)".
|
||||
Hexadecimal is so widespread in programming that programmers often use the term "hex" or "hexadecimal" data to just mean "binary" data, e.g. as in "[hex editor](hex_editor.md)".
|
||||
|
||||
Let's try to make this a bit clearer with a table:
|
||||
Let's try to clarify it all with a table:
|
||||
|
||||
| decimal | binary (2^1) | octal (2^3) | hexadecimal (2^4) |
|
||||
| ------- | ------------ | ----------- | ----------- |
|
||||
|
@ -28,7 +28,7 @@ Let's try to make this a bit clearer with a table:
|
|||
| 17 | 10001 | 21 | 11 |
|
||||
| 18 | 10010 | 22 | 12 |
|
||||
|
||||
The key thing to notice is that a group of 4 binary digits will always directly translate to one hexadecimal digit and vice versa according to the table above, so for example a binary number 00101110 will be converted to hexadecimal number 2E because 0010 translates to 2 and 1110 translates to E. Also notice this doesn't work the same with conversions to/from decimal numbers. As a programmer you should memorize the 16 pairs of hex digits and binary quadruplets so that you can quickly convert numbers in your head.
|
||||
A key observation to make is that a group of 4 binary digits will always directly correspond to one hexadecimal digit and vice versa according to the table above, so for example a binary number 00101110 will be converted to hexadecimal number 2E because 0010 translates to 2 and 1110 translates to E. Also notice this doesn't work the same with conversions to/from decimal numbers. As a programmer you should memorize the 16 pairs of hex digits and binary quadruplets so that you can quickly convert numbers in your head.
|
||||
|
||||
The conversions work as in any other base, basically just remember this: *N*th digit from the right (starting with 0) says how many "16 to N"s there are. So for example a hexadecimal number E0A3 has 3 "16^0"s (1s), 10 (A) "16^1"s (16s), 0 "16^2"s (256s) and 14 "16^3"s (4096s), that's 3 * 1 + 10 * 16 + 0 * 256 + 14 * 4096 = 57507. Is it difficult? No.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue