Couldn't bool be better in C++??
-
-
can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;
-
can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;
tom_dx wrote: wouldn't this be a better solution? not really. it would be a lot slower, since CPUs can't access bits directly. Cleek | Image Toolkits | Thumbnail maker
-
can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;
-
can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;
Back in the old days of C when every byte of memory counted, we used to define a structure of bits so that it would be clearly referenced in code and only use a byte of memory. So, to declare six flags in a byte, you would define a struct like this:
typedef struct _tagBitFlags
{
unsigned char Flag1 : 1;
unsigned char Flag2 : 1;
unsigned char Flag3 : 1;
unsigned char Flag4 : 1;
unsigned char Flag5 : 1;
unsigned char Flag6 : 1;
unsigned char unused : 2;
} BitFlags;Then to access the flags, you could write something like this:
if (BitFlags.Flag1)
printf("Flag1 is set");
else
printf("Flag1 is cleared");Note that the value you can set a flag to must be in the bit range that the structure member can hold. This means that if, as in the above example, Flag1 is declared as one bit, it can only hold the values 0 and 1, whereas unused is declared as two bits and can be assigned the values 0, 1, 2, or 3. Hope this doesn't date me too badly....:~ onwards and upwards...
-
can you just write a class to manipulate bits in an int, so it would contain 32 bool values instead of wasting that one number for 1 or a char for 8 values or double for 64 diff. values, wouldn't this be a better solution? IM PROUD TO BE A GMAIL;
Why don't you try the bitset from STL[^]? You can easely do the following things:
typedef std::bitset<8> Flag8;
typedef std::bitset<11> Flag11;
// etc..Behind every great black man... ... is the police. - Conspiracy brother Blog[^]
-
Back in the old days of C when every byte of memory counted, we used to define a structure of bits so that it would be clearly referenced in code and only use a byte of memory. So, to declare six flags in a byte, you would define a struct like this:
typedef struct _tagBitFlags
{
unsigned char Flag1 : 1;
unsigned char Flag2 : 1;
unsigned char Flag3 : 1;
unsigned char Flag4 : 1;
unsigned char Flag5 : 1;
unsigned char Flag6 : 1;
unsigned char unused : 2;
} BitFlags;Then to access the flags, you could write something like this:
if (BitFlags.Flag1)
printf("Flag1 is set");
else
printf("Flag1 is cleared");Note that the value you can set a flag to must be in the bit range that the structure member can hold. This means that if, as in the above example, Flag1 is declared as one bit, it can only hold the values 0 and 1, whereas unused is declared as two bits and can be assigned the values 0, 1, 2, or 3. Hope this doesn't date me too badly....:~ onwards and upwards...
Not at all. I am glad you typed it in. Saved me some work :~
-
Not at all. I am glad you typed it in. Saved me some work :~
Glad to be of service... and glad that I can remember back that far...:doh: onwards and upwards...