C: Bitmap of every member of an array

Programming, for all ages and all languages.
Post Reply
OSMAN

C: Bitmap of every member of an array

Post 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.)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C: Bitmap of every member of an array

Post 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?
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C: Bitmap of every member of an array

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C: Bitmap of every member of an array

Post 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 :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C: Bitmap of every member of an array

Post 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.)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C: Bitmap of every member of an array

Post 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...
Post Reply