Category : Files from Magazines
Archive   : TREE_DDJ.ZIP
Filename : BITMAP.C
Output of file : BITMAP.C contained in archive : TREE_DDJ.ZIP
.MDNM/ Listing 2 -- bitmap.c
------------------------------------------------------------------------------
1 /* BITMAP.C makebitmap, setbit, testbit: bit map manipulation
2 * routines.
3 *
4 * Copyright (c) 1985,1986 Allen I. Holub, all rights reserved.
5 *
6 * These routines originally appeared in the June, 1985, C Chest (DDJ
7 * #104). They are reproduced here in a stripped-down version.
8 * Please see that column for more information about how they work.
9 */
10
11 extern char *calloc ( unsigned, unsigned );
12
13 typedef char BITMAP;
14
15 /*----------------------------------------------------------------------*/
16
17 BITMAP *makebitmap( size )
18 unsigned size;
19 {
20 /* Make a bit map for "size" bits. */
21 /* Return a pointer to the map or NULL if we couldn't make it. */
22
23 unsigned *map, numbytes;
24
25 numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 ) ;
26
27 if( map = (unsigned *) calloc( numbytes + sizeof(unsigned) ,1 ) )
28 *map = size;
29
30 return (BITMAP *) map;
31 }
32
33 /* ------------------------------------------------------------------- */
34
35 setbit( c, map, val )
36 unsigned c, val;
37 char *map;
38 {
39 /* Set bit c in the map to val. */
40 /* If c > map size, 0 is returned, else 1 is returned. */
41
42 if( c >= *(unsigned *)map ) /* if c >= map size */
43 return 0;
44
45 map += sizeof(unsigned); /* Skip past size */
46
47 if( val ) map[c >> 3] |= 1 << (c & 0x07) ;
48 else map[c >> 3] &= ~(1 << (c & 0x07)) ;
49
50 return( 1 );
51 }
52
53 /* ------------------------------------------------------------------- */
54
55 testbit( c, map )
56 unsigned c;
57 char *map;
58 {
59 /* Return 1 if the bit corresponding to c in map is set. */
60 /* 0 if it is not. */
61
62 if( c >= *(unsigned *)map )
63 return 0;
64
65 map += sizeof(unsigned);
66
67 return( map[ c >> 3 ] & (1 << (c & 0x07)) );
68 }
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/