# How to set individual bits in a bit field

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

## 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 on other sites
You could just use a std::bitset or a std::vector<bool>.

##### Share on other sites
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 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 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 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 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 on other sites
Quote:
 Original post by deksTo 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 on other sites
Quote:
 Original post by King Mirvector 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 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 on other sites
Quote:
 Original post by DrakkconI 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.

Quote:
 The dynamic_bitset class is nearly identical to the std::bitset class. The difference is that the size of the dynamic_bitset (the number of bits) is specified at run-time during the construction of a dynamic_bitset object, whereas the size of a std::bitset is specified at compile-time through an integer template parameter.

##### Share on other sites
Quote:
Original post by bakery2k1
Quote:
 Original post by DrakkconI 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.

Quote:
 The dynamic_bitset class is nearly identical to the std::bitset class. The difference is that the size of the dynamic_bitset (the number of bits) is specified at run-time during the construction of a dynamic_bitset object, whereas the size of a std::bitset is specified at compile-time through an integer template parameter.

Wow, that's just perfect for what I'm doing. Thanks!

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984320

• 23
• 11
• 9
• 13
• 14