Jump to content
  • Advertisement

Archived

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

Orpheum

bit masks

This topic is 6921 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

Does anyone know of a good tut on bit masking? I want to use them in a game I''m working on to indicate whether a tile is passable or not, etc. It would be a hell of a lot more space conscious that using a ton of bool variables! Any info is appreciated!!

Share this post


Link to post
Share on other sites
Advertisement
Do you mean 'instead of 32 bool variables, i've got a long that i want to set and unset bits of, so that I can use each bit as a bool'?

If so, i might be able to help. I warn you though, it might be more trouble than it is worth, you will basically be re-writing the 'array of bools' type...

Basically, what you need to do is to set up functions to set a certain bit in the variable to either 1 or zero. This is accomplished through using boolean logic (you know, AND, OR, NOT etc). So, start with a blank register:

long register = 0x00000000;

this can then be manipulated. To toggle a bit on (sorry about the spacing on this):

void toggle_bit(int num_bit)
{
long mask=0x00000001;
for (int i=0; i {
mask<<1;
}
register = (register ^ mask);
}

(i think the shift is in the right direction.. im away from my compiler)

this will turn bits on and off. To check a bit all you need to do is write a similar function to AND against you register and return true or false.

Im not sure if any of this is completely right, but if i were you i'd plump for bool flags[32]; rather than this method anyday, much easier (and probably faster).There are also MUCH easier ways to do this in inline assembler (i think) but im thick at assembler. Hope this helps...

Clynt.
http://www.joes-garage.fsnet.co.uk/

AHH! I can't format the above code.. the loop should count as far as 32, but i guess you got that.. Damned bulletin board formatting...

Edited by - Clynt on May 11, 2000 4:46:35 AM

Share this post


Link to post
Share on other sites
just as a side note - while you will conserve some memory doing this, your lookups will be a hell of a lot slower.

--
Float like a butterfly, bite like a crocodile.

Share this post


Link to post
Share on other sites
depends on how you do the lookups, you can test for the activeness of more than one bit using only one compare...

But the fact remains that it''s a lot more complex than just having an array of bools, and debugging this stuff if you''ve made a slight typo is a complete pain in the arse


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
quote:
Original post by MadKeithV

depends on how you do the lookups, you can test for the activeness of more than one bit using only one compare...

But the fact remains that it's a lot more complex than just having an array of bools, and debugging this stuff if you've made a slight typo is a complete pain in the arse


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**




Sorry, but I just worked out how to quote

I'm happy now



http://www.geocities.com/ben32768

Edited by - benjamin bunny on May 11, 2000 9:33:54 AM

Share this post


Link to post
Share on other sites
Hehehe good for you! You''ve just surpassed 50% of the message board users in skill

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Why don''t folks just use structured bit fields? This the 2nd or 3rd time since I''ve been reading here that this has come up and everyone goese ''Bitmask! Bitmask!''. In small pieces of test code that I have done a structured bit field and a bitmask used for equivalent flag tracking copiled to the same assembler listing. Setting more bits at once is one reason for bitmasks, but I can have that when needed by unioning a bitmask with the structured bit fields. So why is everyone so bitmask happy?

Mike Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites
Well, I use constants to access bitmasks, eg:

const int CAN_FLY = 1 << 0;
const int CAN_WALK = 1 << 1;
const int CAN_SING = 1 << 2;

And pass those to the toggle/clear/set bit functions.
If I did this as bitfields:

unsigned int can_fly:1;
unsigned int can_walk:1;
unsigned int can_sing:1;

I would have to change the class/structure every time I wished to add or remove a bit, meaning a recompile, etc. As you also pointed out, it also allows me to pass a variable which encapsulates the state of all those fields at once:

int can_fly_and_walk = CAN_FLY / CAN_WALK;

And could use that with my ''set'' function to set 2 bits (or more) at once. You don''t get that conciseness with bitfields. Your union idea is a little odd to me, but whatever works However, I can use the above line of code without ever having to worry about the class''s internal representation, whereas a union is part of the class. And why implement a bitmask -and- bitfields when you can have just 1 simple interface to it?

Share this post


Link to post
Share on other sites
As to the recompile issue when adding a field. So what? You going to rebuild something to to add another constant defined for a particular bit.

The union idea is just a way to get both access methods when you need it. If you don''t need multiple non-contiguous bit setting for the bit fields, the bitfields and bitmasks are exactly the same after compilation(yes I''ve checked this, but maybe you know an example where this isn''t so).

My experience (both for work and fun programming) has been that I have rarely have a situation where a single decision in the code results in the setting more than one bitfield. I definitely find bitfields the more elegant code style.

They also work out nice when you need a four value enum and can put it a 2 bit bitfield (this is a form of your conciseness issue, but only when bits are contiguous).

I don''t find myself worrying about internal representation either. Writing ''states.ThisState = 1;'' seems pretty simple and clear to me. Hell, so does ''states.AnotherState = 2;'' when using a 2 or more bit bitfield. Now that 2nd is a bit more painful () with bitmasks.

Why have an uglier, less intuitive form if most of the time its setting one bit at a time?

At work experience tells me that average programmers (which is not me by the way) have more problem with bitmasks than bitfields. Their just easier for former COBOL programmers to get confused on.

Actually this points out the biggest flaw with C/C++ in my mind. So much flexibility actually gets in the way sometimes for interprogrammer discussions/code sharing/etc. Damnit, code my style!

Mike Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites
unsigned int tiles_passable[256*256 / 32];

void SetPassable(int iNr)
{
__asm mov eax, iNr
__asm mov ecx, 1
__asm and eax, 0x0000001F
__asm mov ebx, iNr
__asm rol ecx, eax
__asm shr ebx, 5
__asm or tiles_passable[ebx * 4], ecx
}

to clear a bit, change 2nd and last asm line to:
__asm mov ecx, 0xFFFFFFFE
__asm and tiles_passable[ebx * 4], ecx

to toggle a bit, change last asm line to:
__asm xor tiles_passable[ebx * 4], ecx

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!