Allocating a Bit

Started by
7 comments, last by Peaceman 21 years, 11 months ago
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
Advertisement
I''m afraid you are out of luck. You cannot allocate individual bits.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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 =)
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
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
D.V.Carpe Diem
If you are brutal, and know ASM, there is probably a way to do it.

Proceeding on a brutal rampage is the obvious choice.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
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 bitbitmask &= ~FLAG_1;        // Unset least significant bitbitmask = FLAG_2 | FLAG_3; // Set bits 2 and 3 onlyif(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]
Well, as I need as much speed as possible (don''t we all need?) I''ll just have to stick with bools.

Thanks for your replys anyway.
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.

This topic is closed to new replies.

Advertisement