知识点7:十六进制数(hexadecimal)

We have this notion of the hexadecimal system, which is base 16, instead of base 10 or base 2. Which means that we have 16 digits to work with instead of 10 or 2.

And it's a much more concise way to express binary information on a computer system, it's much more human understandable.

知识点7:十六进制数(hexadecimal)_第1张图片

So why is hexadecimal cool? Why do we need to use this other additional base? We already have 2 and 10, why do we need 16?

Well 16 is a power of 2(16是2的幂 ), and so each hexadecimal digit, 0 through f, corresponds to a unique ordering, or unique arrangement of 4 binary digits, 4 bits.

And so in that sense, we can express very long, complex, binary numbers in hexadecimal in a much more concise way, without losing information or having to do particularly cumbersome conversions on those numbers.


Generally when we are expressing numbers into hexadecimal notation to clearly distinguish them from decimal notation, we usually prefix them with the prefix 0x.

知识点7:十六进制数(hexadecimal)_第2张图片

0x means nothing in reality, it's just a clue to us as humans that what we're about to see, or about to start parsing, is a hexadecimal number.


hexadecimal is no exception here, really. It also has place values but instead of being powers of 2 or powers of 10, they're powers of 16.

This is the hexadecimal number three-nine-seven:

知识点7:十六进制数(hexadecimal)_第3张图片

It's not 397, it means something different, because we're using powers of 16 as all of our place values instead of powers of 10.

知识点7:十六进制数(hexadecimal)_第4张图片

In fact, the place values here would be the ones place, the sixteens place, and the two-hundred-fifty-sixes place, which correspond to our idea of a ones place, tens place, and a hundreds place, if the number was 397. But since it's 0x 397, we have a ones place, sixteens place, and a two-hundred-fifty-sixes place.

知识点7:十六进制数(hexadecimal)_第5张图片

Or, a 16 to the 0 place, which is 1. A 16 to the first power place, 16. A 16 squared place, 256, and so on, and so on, and so on.

So this number is really 3 times 16 squared, plus 9 times 16, plus 7. I didn't do the math here, but it's not 397, it's much, much larger than that.

知识点7:十六进制数(hexadecimal)_第6张图片

Similarly, we could have 0x adc, well that's a times 16 squared. Or if we translate that to our notion of decimal numbers, that's 10 times 16 squared, plus d times 16, or plus 13 times 16.

And don't worry if you haven't memorized that d is 13, or anything like that, there's not too many of these letter digits and it'll become intuitive pretty quickly. So again this is 10 times 16 squared, plus 13 times 16, plus 12 times 1. So 0x adc.


we use hexadecimal quite a lot as programmers. Not necessarily for the purpose of doing math with it, but because a lot of times memory addresses in our system are represented in hexadecimal.

It's a really concise way to express otherwise cumbersome, binary numbers.

你可能感兴趣的:(知识点7:十六进制数(hexadecimal))