Page 1 of 1

C: Bitmap of every member of an array

Posted: Mon Jun 05, 2006 1:17 pm
by OSMAN
Hi.
If I have, for example, a pointer to an array of unsigned ints, how can I make bitmaps of them, for I could then call:

Code: Select all

array[i].bitx=1; // or 0
?
(I have a bitmap structure type of the size of unsigned int.)

Re:C: Bitmap of every member of an array

Posted: Tue Jun 06, 2006 12:16 am
by Solar
Just guessing, but shouldn't making it into an array of those bitmaps (instead of an array of ints) do the trick already?

Re:C: Bitmap of every member of an array

Posted: Tue Jun 06, 2006 10:36 am
by Candy
You could try (if you actually use a C++ compiler) to make an array of bools and to see whether it optimizes them to a bit vector (is that allowed Solar?).

You could also make a class to encapsulate bit vector behaviour (or make a vector<bool> instead), again if you use c++.

As a final idea, make a few encapsulating access functions and a generic type of pointer to the array, which makes the implementation abstract.

Code: Select all

// solar, this next line is not for you
typedef int *bit_vector;

bit_vector create_bit_vector(int bits) {
   return (bit_vector)malloc((bits + 7) / 8);
}

int get_bit(bit_vector v, int bitno) {
   // do magic
   return v[bitno / 8] >> (bitno % 8);
}

int set_bit(bit_vector v, int bitno, /*bool*/int on) {
   // do magic
   register int byteno = bitno / 8;
   v[byteno] = (v[byteno] & (1 << (bitno % 8))) | (on << (bitno % 8));
}

void free_bit_vector(bit_vector v) {
   free(v);
}
This code is of yet untested, but will be added to atlantisos I think :D

Re:C: Bitmap of every member of an array

Posted: Tue Jun 06, 2006 10:38 am
by Candy
OSMAN wrote: Hi.
If I have, for example, a pointer to an array of unsigned ints, how can I make bitmaps of them, for I could then call:

Code: Select all

array[i].bitx=1; // or 0
?
(I have a bitmap structure type of the size of unsigned int.)
If you intend to make an array of structs:

Code: Select all

struct nice_struct{
   int bitx:1, bity:1;
   int value:3, valuetoo:3;
};

nice_struct array[42];

int main() {
   array[0].bitx = 1;
}
Sorry if the previous post is pointless for your question :)

Re:C: Bitmap of every member of an array

Posted: Wed Jun 07, 2006 12:22 am
by Solar
Candy wrote: You could try (if you actually use a C++ compiler) to make an array of bools and to see whether it optimizes them to a bit vector (is that allowed Solar?).
Allowed? Actually, the standard requires vector<bool> to optimize to a bit vector. However, a vector<bool> does no longer behave like a standard vector, which is why it has fallen from grace some time before.

A good link on what you should know about vector<bool>. It is deprecated, and future standard versions might no longer have this specialization (which means your code would suddenly break when compiled with a newer C++ library.)

Re:C: Bitmap of every member of an array

Posted: Wed Jun 07, 2006 2:21 pm
by Candy
Solar wrote:
Candy wrote: You could try (if you actually use a C++ compiler) to make an array of bools and to see whether it optimizes them to a bit vector (is that allowed Solar?).
Allowed? Actually, the standard requires vector<bool> to optimize to a bit vector. However, a vector<bool> does no longer behave like a standard vector, which is why it has fallen from grace some time before.

A good link on what you should know about vector<bool>. It is deprecated, and future standard versions might no longer have this specialization (which means your code would suddenly break when compiled with a newer C++ library.)
That's exactly my point, that it was NOT a vector. It's an array:

Code: Select all

bool array[256];
Is this an array that takes 32 bytes or an array that takes 256/512/1024 bytes?


I am fully aware of the vector special case, being so that I've started making a C++ STL and the vector was my first test subject for implementing. You commonly start (or should start) with the header, hence...