Example 1:
#include <stdio.h>
int main()
{
int a;
// Note: a++ is a shortcut for "a = a + 1"
for (a = 0; a < 16; a++)
{
printf("a has the value %d (or 0x%x)\n", a, a);
}
return 0;
}
Example 2:
#include <stdio.h>
#include <stdint.h> // needed for uint8_t definition
int main()
{
uint8_t var;
uint8_t shifted_var;
var = 0x6e;
shifted_var = (var >> 1);
printf("var shifted right 1 bit is 0x%x\n", shifted_var);
shifted_var = (var << 2);
printf("var shifted left 2 bits is 0x%x\n", shifted_var);
return 0;
}
Example 3:
#include <stdio.h>
#include <stdint.h> // needed for uint8_t definition
int main()
{
/* Pretend reg has the following bits:
7 6 5 4 3 2 1 0
PR3 PR2 PR1 PR0 AR1 AR0 SB1 SB0
value: 1 1 0 1 0 1 0 1 */
uint8_t reg = 0xd5;
uint8_t ar_bits;
// Let's try to extract AR1 and AR0...
ar_bits = (reg & 0x0c);
ar_bits = ar_bits >> 2;
printf("AR bits are 0x%x\n", ar_bits);
return 0;
}