Programming-Arduino (1) - Pages-140
Programming-Arduino (1) - Pages-140
A bit is a single digit of binary information, that is, either 0 or 1. The word bit is
a contraction of binary digit. Most of the time, you use int variables that actually
comprise 16 bits. This is a bit wasteful if you only need to store a simple
true/false value (1 or 0). Actually, unless you are running short of memory, being
wasteful is less of a problem than creating difficult-to-understand code, but
sometimes it is useful to be able to pack your data tightly.
Each bit in the int can be thought of as having a decimal value, and you can
find the decimal value of the int by adding up the values of all the bits that are a
1. So in Figure 7-2 , the decimal value of the int would be 38. Actually, it gets
more complicated to deal with negative numbers, but that only happens when the
leftmost bit becomes a 1.
When you are thinking about individual bits, decimal values do not really
work very well. It is very difficult to visualize which bits are set in a decimal
number such as 123. For that reason, programmers often use something called
hexadecimal , or, more commonly, just hex . Hex is number base 16. So instead
of having digits 0 to 9, you have six extra digits, A to F. This means that each
hex digit represents four bits. The following table shows the relationship among
decimal, hex, and binary with the numbers 0 to 15: