Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

How to set individual bits in a bit field

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm writing a memory manager for my game engine (for purely educational purposes), and I've hit a wall. The way I divide up my heap is into segments, where each segment is divided into pages, and each page is divided into homogenously sized memory blocks. Each segment page has a bitmap, where each bit is associated with a block of memory, to determine whether that memory is allocated or not. The problem is that I can't find a way to set a particular bit without clobbering the byte in which it is stored. I have two requests: Could you define a function for me in C++ which takes a byte, a number which represents a bit within that byte to modify, and a boolean value which will be used to set the bit? Could you please explain this function? Could you tell me if this is a good way of keeping track of memory allocations. Are there better ways? I have seen ways involving the map data structure, but that seems like it require a lot of overhead. Thanks in advance for any replies.

Share this post


Link to post
Share on other sites
Advertisement
I can't use a bitset because the HeapPage class is not templatized, or otherwise knowing how big it's going to be at compile time. I can't use std::vector<bool> because at some point I may overload global new, and I'm not sure how a vector uses it.

Also, I'm just very curious as to how this would be done.

Edit: I suppose I could make my page templatized and use a dynamic array of bitset<sizeof(T)>'s, but I think I could find a more elegant solution.

Share this post


Link to post
Share on other sites
std::vector will use an allocator you pass it for it's memory management. You can create an allocator that just calls malloc/free, which will bypass global operator now.

Share this post


Link to post
Share on other sites
Oh, okay, I had forgotten that you could do that, and it seems like a good idea. I will probably end up using a vector<bool> because I prefer using container classes to arrays.

Still though, I'd like to know.

Share this post


Link to post
Share on other sites
To set a bit: value |= (1 << bit);
To clear a bit: value &= ~(1 << bit);
To test if a bit is set: value & (1 << bit) ? true : false;

Share this post


Link to post
Share on other sites
vector<bool> is depreciated. You really should use bitset if anything.

What deks said applies to bitsets as well as privative types.

Share this post


Link to post
Share on other sites
Quote:
Original post by deks
To set a bit: value |= (1 << bit);
To clear a bit: value &= ~(1 << bit);
To test if a bit is set: value & (1 << bit) ? true : false;


Well that clears it up, thanks. I had actually had a function that worked, but was much too complicated to make inline (I did a lot of funky stuff to isolate the bit), and seeing you put it so simply really helped. Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by King Mir
vector<bool> is depreciated. You really should use bitset if anything.

What deks said applies to bitsets as well as privative types.


Has it been actually deprecated, or is it still just wishful thinking?

Share this post


Link to post
Share on other sites

-BitField Arrays-

for bytes:

unsigned int index = bitIndex >> 3;
unsigned int mask = 1 << (bitIndex & 3);

set: bitFieldArray[index] |= mask;
clear: bitFieldArray[index] &= ~mask;
test: (bitFieldArray[index] & mask) != 0;

for dword:

unsigned int index = bitIndex >> 5;
unsigned int mask = 1 << (bitIndex & 31);

// same set/clear/test, except the array is made up of dword-sized elements.

// For optimal speed, ensure bitIndex is unsigned, otherwise the compiler will add some useless instructions to the machine code output.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!