samedi 27 juin 2015

How can I use Bit-Fields to save memory?

This is about ANSI-C (C90).

This is what i know: I can directly tell the compiler how many bits i want for a specific variable. If I want 1 bit which can have the values zero or one. or 2 bits for the values 0,1,2,3, and so on...;

I'm familiar with the syntax.

I have problem concering bitfields: If I want to define a SET structure. It can have maximum 1024 elements(can have less. but max is 1024 elements) And the domain of the set is from 1 to 1024 . so an element could have value 1-1024.

I'm trying to create a structure for a SET. and it must be efficient as possible, for the memory part.

I tried :

typedef struct set
{
    unsigned int var: 1;
} SET;
//now define an array of SETS
SET array_of_sets[MAX_SIZE]  //didn't define MAX_SIZED , but no more than 1024 elements in each set.

I know this isn't effiecient, maybe its even not good for what I want, that's why I'm looking for help.

thanks.

Aucun commentaire:

Enregistrer un commentaire