0% found this document useful (0 votes)
10 views

Programming-Arduino (1) - Pages-140

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Programming-Arduino (1) - Pages-140

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Bit Manipulation

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.

Figure 7.2 An int .

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:

Decimal Hex Binary (Four Digit)


0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101

You might also like