Programming Arduino Getting Started with Sketches



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə55/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   51   52   53   54   55   56   57   58   ...   78
Arduino SimonMonk 2011

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.
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:


So, in hex, any 
int
can be represented as a four-digit hex number. Thus, the binary
number 10001100 would in hex be 8C. The C language has a special syntax for using hex
numbers. You can assign a hex value to an 
int
as follows:
The Arduino standard library provides some functions that let you manipulate the 16
bits within an 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   51   52   53   54   55   56   57   58   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin