Archived

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

1 bit BOOL

This topic is 5328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Will I see any gains by creating my own BOOL type that''s 1 bit in length instead of the 1byte built-in bool type???
typedef std::bitset<1> opt_bool  //Optimized BOOL - 1 bit

// Define true and false 1 & 0 so we can use true/false in code.
#define true  1
#define false 0

It takes a little tweeking of conditional statements but would I see any benefit from using my own optimized BOOL -- If my classes contain quite a few BOOL variables won''t this reduce the size of my classes and the speed...???? Rocco is the Boy!

Share on other sites
Computers aren't any good at accessing values less than one byte. Simulating booleans that way would be a lot slower.

I'm not sure how the bitset is implemented, but it's probably not any smaller than a one byte bool, either. =/

[edited by - twix on July 25, 2003 10:08:57 PM]

Nope.

Share on other sites
You do realize using #define like that simply will replace every "true" with 1 and "false" with 0, which is what a bool is anyway. A bool is 1 byte. You are replacing it with what the compiler will probably interpret as an int, which is 4 bytes. All you will do is make your program slower. In fact, probably not, since if your bottleneck is testing bools you don't need it to run any faster anyway.

I'm still amazed by this post...

 The compiler can do mega optimizations knowing that the type is a bool. There is know way you will speed it up by writing your own type.

[edited by - Raloth on July 25, 2003 10:18:33 PM]

Share on other sites
I am guessing the sizeof your bitset will still be 4 bytes.

Share on other sites
If space is an issue to you then create an unsigned int in your class and use it as a bitfield e.g. to access the bits you would

#define BIT_ONE 0x00000001

DWORD dwFlags = 0;

// Assigning a bit.
dwFlags |= BIT_ONE;

// testing a bit
if(dwFlags & BIT_ONE)
{
...
}

I have done this a lot for databases where I want a lot of boolean flags

Share on other sites
@Raloth
Don''t get bent outta shape, I''m just asking - apparently std::bitset<1> is an INT which boggles me since the documentation says it makes 1-bit - so I couldn''t use that anyway - and the reason for the #define statements is b/c my VC++6 gave me errors when trying to use this opt_bool until I made it explicitly 1 & 0 values for true/false...

So I can''t use std::bitset to create a 1-bit anything but I guess I don''t need to since it won''t help out anyway -- The whole reason I asked is b/c some of my classes are inheriting and are ending up with like 10 - 12 BOOL''s on top of the variables they already have.

Share on other sites
BOOL is not a C++ boolean data type, it is a Microsoft typedef for int. The C++ boolean type is bool and can take exactly two values, namely true and false.

quote:
Original post by OneStiffRod
// Define true and false 1 & 0 so we can use true/false in code.
#define true 1
#define false 0

It is explicitely forbidden to #define keywords (C++ Standard, clause 17.4.3.1.1-1). true and false are off-limits to you.

It is pointless to worry about the size of one bool, and there are containers (std::bitset and std::vector<bool> which offer space-efficient storage of many bools.

Or you may use bitfields :
struct foo{  bool a : 1;};

Of course, since the minimum addressable element is one byte, your structure will still be at least one byte long (+padding). It only becomes interesting when you have more than one bitfield member.

Note : sizeof(bool) is implementation-defined (C++ Standard, clause 5.3.3-1). It may or may not be 1 byte.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share on other sites
I''ll just use some FLAGS like the last post suggested...

Share on other sites
that would work very nicly but you would have to be for a situation like this:

struct myData {  bool a, b, c, d, ... z;};//can be replaced but thisstruct myData {   std::vector<bool> stuff(26); //compiles to a std::bitset};

the above 26 bools would "compress" down to a 4 byte integer (instead of 26 x 1byte bools as the first struct)

something like 26 bools right (if you have like 4 dont bother or use a single long or Dword and create a simple bitset class )

i think you would see a performace increase because CPUs are good
at bitshifting, but ONLY if you have large amounts of bools, (i
saw a 200% increase in some 3d applications i have written where
the bitset replaced ~1500 booleans)

(just incase) a bitset works like this:
int mybitset; //a 4byte int has 8x4 bits//what we want to do is use each bit as a "bool" either on or off//to access each bit we shift by an ENUM Setting (or n)enum Settings {  SETTING_A = 1,   SETTING_B = 2,  SETTING_C = 3,};if( mybitset & (1 << (SETTING_A & 31 )) ){   // do something cuz this is tru..}

what that does is it checks to see if the bit is on or off.
NOTE: smallest possible size for a bitset is 1 byte

Programmer Four - try tellling him WHY (see the title "for BEGINNERS")

A GOOD friend will come bail you out of jail...
but, a TRUE friend will be sitting next to you saying, "Damn, we fucked up."
Ingite 3D Game Engine Home -- Just click it

Share on other sites
Technically, C++ cannot allocate anything smaller than a char which generally 1 byte (but doesn''t have to be!)

Bitfields are really just a shorthand way of doing bit operations on a larger amount of data. As someone else pointed out, they are really only useful if you need lots of them and want to save some space.

Share on other sites
Thnx to: SilverMace && Anonymous

That explains it pretty good -- I think I can reduce my boolean values a ton by using integer flags -- this way I can just put one int value in my classes and AND the flags together to get the boolean settings - that should create significant savings.

The classes themselves aren''t that consuming space or time wise but if you have a 1000 or more instances of them even the tiny flaws showup big.

np

Share on other sites
quote:
Original post by silvermace
Programmer Four - try tellling him WHY (see the title "for BEGINNERS")

We can''t all be all right all the time. For instance, you have three l''s in "telling"