2 * bitmap: An example of bitmap handling
7 #include <readline/readline.h>
8 #include <readline/history.h>
11 #define COLS (sizeof(unsigned char) * 8)
13 static unsigned char bitmap
[LINES
];
15 /* bitmap_bit_is_set(): Check if bit 'num' is set.
16 * Return 0 if bit is set, 0 otherwise */
17 static int bitmap_bit_is_set(int num
)
19 return (bitmap
[num
/ COLS
] & (1 << (num
% COLS
)));
22 /* bitmap_set_bit(): Set bit 'num' */
23 static void bitmap_set_bit(int num
)
25 bitmap
[num
/ COLS
] |= 1 << (num
% COLS
);
28 /* bitmap_unset_bit(): Unset bit 'num' */
29 static void bitmap_unset_bit(int num
)
31 bitmap
[num
/ COLS
] &= ~(1 << (num
% COLS
));
34 /* bitmap_print(): Print bitmap on stdout
36 * Note that the map is printed on the reverse order.
37 * For example, each word in the map is stored in
44 * Where bit number 0 is the LSB one and bit number 7
47 * But, when printing, we start to print from bit number
48 * 0 (the LSB) which gives the following screen for all
57 * See? We start printing from bit 0 to bit 7.
59 static void bitmap_print(void)
61 unsigned int count
, j
, i
;
63 for (i
= 0; i
< COLS
; i
++)
68 for (i
= 0; i
< (LINES
* COLS
); i
++) {
69 printf("%d ", bitmap_bit_is_set(i
) ? 1 : 0);
70 if (++count
== COLS
) {
71 printf("[%#x]\n", bitmap
[j
++]);
77 static void help(void)
80 "clear clear bitmap\n"
81 "exit exit from program\n"
83 "print print bitmap\n"
84 "set <hex value> set bit correspondent to value\n"
85 "unseset <hex value> unsetset bit correspondent to value\n"
94 cmd
= readline(">>> ");
100 if (!memcmp(cmd
, "clear", 5)) {
101 memset(bitmap
, 0, LINES
* COLS
);
102 } else if (!memcmp(cmd
, "exit", 4)) {
105 } else if (!memcmp(cmd
, "help", 4)) {
107 } else if (!memcmp(cmd
, "print", 5)) {
109 } else if (!memcmp(cmd
, "set", 3) ||
110 (!memcmp(cmd
, "unset", 5))) {
112 char *p
= strchr(cmd
, ' ');
114 printf("Invalid use of set or unset\n");
116 num
= strtol(p
, (char **) NULL
, 16);
120 bitmap_unset_bit(num
);
123 printf("Invalid command: %s\n", cmd
);