Archived

This topic is now archived and is closed to further replies.

lephyrius

Defining an variable?

Recommended Posts

lephyrius    355
I want to define a varable that is 2-bit(only 4 combinatation but its sufficient) so how do i make one....? I need alot of memory free for this application!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
depends on the system, but usually the smallest data type is a byte (8bit). you can make a variable of type "char", so four of your 2bit-values fit into the byte. you have to bishift <</>> with the position of the value to load/store the correct values.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Actually a short is typically 16bit. At least on all 32bit OS''s.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
2 byte = 16 bit

dah


yes but the man wants 2 bit not 16 bit

Share this post


Link to post
Share on other sites
lephyrius    355
I think i have found it i use bitset:

typedef std::bitset<2> 2bit;

//my variable

2bit myvariable = 4;

shouldn''t it work or did i get it wrong...?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Can''t begin a variable name (2bit) with a number...

Share this post


Link to post
Share on other sites
rypyr    252
struct MyStruct {
unsigned int twobits : 2;
};

Although this structure may take up more space, twobits can only contain values from 0 to 3.

The std library does provide another option, but I''ve never used it.

Regards,
Jeff

Share this post


Link to post
Share on other sites
NeonGE    140
I create structures for this kind of stuff (maybe dont help, but maybe yes so...)


  
struct Anything
{
UCHAR OneBitThing:1;
UCHAR TwoBitsThing:2;
UCHAR FourBitsThing:4;
UCHAR InUseBit:1;
};


Well, the structure use 8 bits (unsigned char) but i can use them like each bit is a diferent variable (useful for terrain and entities structures)

If God with me, Who against me?

Share this post


Link to post
Share on other sites
666_1337    122
you could use something like this:

class twoBitType
{
private:
char *values;
public:
twoBitType(int length);
void set(int index, int newValue);
bool isSet(int index);
};


  
twoBitType :: twoBitType(int length)
{
this -> values = (char*)malloc((int)(length/4 + 0.8f));
}

void twoBitType :: set(int index, int newValue)
{
assert(newValue < 4);
//i don''t care whether you like intel or gnu syntax better. this is gnu

mov index, %eax
shr $2, %eax
mov values, %edx
add %eax, %edx
mov newValue, (%edx)
}
bool twoBitType :: isSet(int index)
{
mov index, %eax
shr $2, %eax
mov values, %edx
add %eax, %edx
mov (%edx), %eax
or %eax, %eax // "or %eax, %eax" is zero if eax was zero. the returning value will be expected to be stored in (e)ax by the c standart

ret
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Just curious lephyrius, but what are you using this for? I''m not sure how std::bitset is implemented, but I''d be very surprised if it actually saved any memory over a char. If you just want to keep the possible values within a small interval, an enum might be more flexible. Or just use a char and be done with it

Share this post


Link to post
Share on other sites