Better name for examples dir
[lcapit-junk-code.git] / snippets / C / bitwise.c
blob79e1ac4c5dfd4a1919809891bbcffd50e937ab32
1 /*
2 * An example of bitwise operations in C
3 *
4 * Luiz Fernando N. Capitulino <lcapitulino@mandriva.com.br>
5 */
6 #include <stdio.h>
8 static void show(unsigned char var)
10 printf("-> %#x\n", var);
13 static void set_bit(unsigned char *var, unsigned char bits)
15 *var |= bits;
18 static void unset_bit(unsigned char *var, unsigned char bits)
20 *var &= ~bits;
23 static void left_shift(unsigned char *var, int positions)
25 *var <<= positions;
28 static void right_shift(unsigned char *var, int positions)
30 *var >>= positions;
33 int main(void)
35 unsigned char var = 0;
37 show(var);
39 set_bit(&var, 0x1);
40 show(var);
42 set_bit(&var, 0x8);
43 show(var);
45 set_bit(&var, 0x80);
46 show(var);
48 unset_bit(&var, 0x8);
49 show(var);
51 left_shift(&var, 2);
52 show(var);
54 right_shift(&var, 2);
55 show(var);
57 return 0;