You've learned about decimal numerals (the base 10 system with ten digits, 0-9, that uses powers of ten for place values) and binary numerals (the base 2 system with two digits, 0 and 1, that uses powers of two for place values).
Another common number system used with computers is hexadecimal...
10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|
A | B | C | D | E | F |
A hexadecimal numeral is part of the base 16 system.
In base 16, there are sixteen digits (0-9 and A-F), and each place is worth sixteen times as much as the place to its right.
For example, consider the hexadecimal numeral 2D9...
Base 16 uses powers of sixteen instead of powers of two or ten. Place values in hexadecimal represent the units place (160 = 1), the sixteens place (161 = 16), the two-hundred-fifty-sixes place (162 = 256), etc. So, for example:
3B716 = 3 × 162 + 11 × 161 + 7 × 160 = 768 + 176 + 7 = 95110
Computers store information in binary as on and off signals (ones and zeros), but for computer programmers, typing long strings of ones and zeros is inconvenient and prone to typos. Hexadecimal (also known as "hex") numbers require less typing and are easier to type correctly.
Since 16 is a power of 2 (24 = 16), 4 bits fit inside one hex digit. For example, the binary numeral 1101 is (1 × 8) + (1 × 4) + (0 × 2) + (0 × 1) = 13 in decimal or just D in hex.
Click for a hint about using Snap! to check your work with hex.
You will need to nest the binary and hex conversion blocks above with the and
blocks in order to get from hex to decimal.
Why do the input slots to binary of hex
and hex of binary
have different shapes?
Snap! uses the shape of the input slot to tell Snap! and the programmer what kind of inputs are expected.
The three color values of RGB pixels each range from 0 to 255 because eight bits are used for each of the three colors and 28 = 256. But in hexadecimal, 162 = 256, so it only takes two hex digits to represent each of the RGB color values (0 to 255 in decimal is 00 to FF in hex).
As you've seen, if all three color values are as bright as possible, we see white, and if they are as dark as possible, we see black. Instead of writing (255, 255, 255) for white and (0, 0, 0) for black, we often use hex notation: FFFFFF and 000000.
Also (128, 0, 255) = 8000FF is the color is purple: some red (128 = 8016) and as much blue as possible (255 = FF16), but no green at all (0 = 0016). And this color is red 255, green 127, and blue 0, which is FF7F00 in hex.