Programming-Arduino (1) - Pages-141
Programming-Arduino (1) - Pages-141
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
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:
int x = 0x8C;
As well as using hex notation for numbers, you can also use binary notation
directly using the prefix ‘0b’. For example, the binary used in the hex example
of 0x8C could be written directly in binary as:
0b10001100
The Arduino standard library provides some functions that let you
manipulate the 16 bits within an int individually. The function bitRead returns
the value of a particular bit in an int ; so, for the following example would
assign the value 0 to the variable called bit :
int x = 0b10001100;
In the second argument, the bit position starts at 0 and goes up to 15. It starts
with the least significant bit. So the rightmost bit is bit 0, the next bit to the left is