patterncMinor
Bitmap implementation
Viewed 0 times
implementationbitmapstackoverflow
Problem
I recently started cleaning up my projects and decided that I want to create a personal repository of useful bits of code. I found this bitmap code between them and heavily edited it and I'd appreciate if someone could review it, because I'm not that used to bitwise operations and such. Is the code portable enough, or there should be changes?
bitmap.h
bitmap.c
bitmap.h
#ifndef BITMAP__
#define BITMAP__
#define BIT (8*sizeof(byte))
#define BITMAP_NOTFOUND -1
typedef enum{false=0, true} bool;
typedef unsigned char byte;
bool bitmapGet (byte *, int);
void bitmapSet (byte *, int);
void bitmapReset (byte *, int);
int bitmapSearch(byte *, bool, int, int);
#endifbitmap.c
#include "bitmap.h"
static bool get (byte, byte);
static void set (byte *, byte);
static void reset(byte *, byte);
/* CAREFUL WITH pos AND BITMAP SIZE! */
bool bitmapGet(byte *bitmap, int pos) {
/* gets the value of the bit at pos */
return get(bitmap[pos/BIT], pos%BIT);
}
void bitmapSet(byte *bitmap, int pos) {
/* sets bit at pos to 1 */
set(&bitmap[pos/BIT], pos%BIT);
}
void bitmapReset(byte *bitmap, int pos) {
/* sets bit at pos to 0 */
reset(&bitmap[pos/BIT], pos%BIT);
}
int bitmapSearch(byte *bitmap, bool n, int size, int start) {
/* Finds the first n value in bitmap after start */
/* size is the Bitmap size in bytes */
int i;
/* size is now the Bitmap size in bits */
for(i = start+1, size *= BIT; i > pos) & 1;
}
static void set(byte *a, byte pos) {
/* pos is something from 0 to 7*/
/* sets bit to 1 */
*a |= 1 << pos;
}
static void reset(byte *a, byte pos) {
/* pos is something from 0 to 7*/
/* sets bit to 0 */
*a &= ~(1 << pos);
}Solution
If you expect your code will ever be used from C++ (which a lot of C code is) then you should not use any identifiers with a double underscore.
As this is reserved for the implementation in C++. Also the term BITMAP is very generic and you are very likely to clash with other libraries try and make it more unique to your project.
You are assuming that a char is 8 bits.
The standard does not say this (it is at least 8 bits) but it is actually defined via the macro CHAR_BITS so you should use:
Your header file is how most people will first see your code and try and understand how to use it. Thus putting the parameter names into the header file is probably a good idea as it helps document their usage.
Additional the action of
Be careful with comments:
This is only true if you make assumptions (A: that sizeof(byte) == 1 B: you multiply this by 8). The comment should have been:
Note the use of '[' on the left and ')' on the right. It is mathematical notation indicating that 0 is inclusive and BIT is not included in the range (it is common in both C/C++ documentation).
#ifndef BITMAP__
// ^^ May cause problems in C++As this is reserved for the implementation in C++. Also the term BITMAP is very generic and you are very likely to clash with other libraries try and make it more unique to your project.
You are assuming that a char is 8 bits.
#define BIT (8*sizeof(byte))The standard does not say this (it is at least 8 bits) but it is actually defined via the macro CHAR_BITS so you should use:
#define BIT (CHAR_BITS*sizeof(byte))Your header file is how most people will first see your code and try and understand how to use it. Thus putting the parameter names into the header file is probably a good idea as it helps document their usage.
bool bitmapGet (byte *, int);
void bitmapSet (byte *, int);
void bitmapReset (byte *, int);
int bitmapSearch(byte *, bool, int, int);Additional the action of
bitmapSearch is not obvious thus a comment on its usage in the header would probably be a good idea. Actually a small blurb about how the first parameter can be an array would probably be a good idea (as it is not obvious without reading the code).Be careful with comments:
/* pos is something from 0 to 7*/This is only true if you make assumptions (A: that sizeof(byte) == 1 B: you multiply this by 8). The comment should have been:
/* pos is a value between [0, BIT) */Note the use of '[' on the left and ')' on the right. It is mathematical notation indicating that 0 is inclusive and BIT is not included in the range (it is common in both C/C++ documentation).
Code Snippets
#ifndef BITMAP__
// ^^ May cause problems in C++#define BIT (8*sizeof(byte))#define BIT (CHAR_BITS*sizeof(byte))bool bitmapGet (byte *, int);
void bitmapSet (byte *, int);
void bitmapReset (byte *, int);
int bitmapSearch(byte *, bool, int, int);/* pos is something from 0 to 7*/Context
StackExchange Code Review Q#8213, answer score: 7
Revisions (0)
No revisions yet.