Archived

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

Peaceman

Allocating a Bit

Recommended Posts

Peaceman    122
Hi, I would like to have a value which is capable of holding 1 or 0. However a bool uses a whole byte or even 2, which seems like a waste to me. How can a alocat only a bit? thx

Share this post


Link to post
Share on other sites
NafdahliX    205
emmm i think that would be impossible since the smallest adressable memory "chunk" is a byte (8 bits)
though i think there are some special CPU registers that hold 1 bit only dunno if they can be used.

someone plz tell me if im wrong =)

Share this post


Link to post
Share on other sites
siaspete    208
You can use bitfields, but your program will be faster if you just use an int.

Your OS probably can''t allocate memory in non 4 byte aligned chunks anyway so just deal with it. It''s a good thing, really.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
DeltaVee    138
FYI: Actually, depending on your CPU architecture and heap management, even if you allocate 1 byte, the OS is going to return a chunk of memory considerable larger than 1 byte.


D.V.

Carpe Diem

Share this post


Link to post
Share on other sites
cgoat    132
You can use bitfields, but there's a few catches:

1) They can only be used in structures.
2) Your structure is going to take up at least 4 bytes no matter what you do.
3) They're slower than using ints, as mentioned earlier.
4) If you're copying some other value into this structure, you need to know the layout of bytes in memory (big-endian, little-endian)

Here's how you'd create it:

struct _bitfields
{
unsigned bits_one:1; // One bit
unsigned bits_three:3; // 3 bits
};


So in short, it's not really all that useful. If you want to create bitmasks, do it with #defines and an unsigned int:

#define FLAG_1 0x0001
#define FLAG_2 0x0002
#define FLAG_3 0x0004
#define FLAG_4 0x0008
// etc...


unsigned int bitmask;


bitmask |= FLAG_1; // Set least significant bit
bitmask &= ~FLAG_1; // Unset least significant bit
bitmask = FLAG_2 | FLAG_3; // Set bits 2 and 3 only
if(bitmask & FLAG_1) // Check if least significant bit set


This is basically what Microsoft does in a lot of their Win32 functions.

Edit: Fixed some stuff

[edited by - cgoat on April 30, 2002 5:53:02 PM]

Share this post


Link to post
Share on other sites
a person    118
depending on why you need the 1 bit is on how you sohuld deal with it. if you its for a true false return, thne you must use bool, char, short, int, long, (singned or unsigned). if its for holding flags, then using a DWORD along with some constants is better (and faster). the reason is because you can load 32 flag statuses at once, and check them using a single AND
(ie
if(flags&FLG_SHOULDDRAW)
doSomething();
}
you could also place them in a switch statement. with muitple bools holding flags, you need to load each one before using it which means less efficent register usage, more stack manipulation, more reads from the data cache (and memory). while you dont actually see all that going on since the compiler and cpu take care of it, you should still be concerned about it.

Share this post


Link to post
Share on other sites