Coding help with bitfields?

Started by
8 comments, last by gimp 23 years, 11 months ago
Currently to store my bits for the packet data I''m using defines to set individual bits. Like this: #define NET_MOVE_FORWARD 1 #define NET_MOVE_BACKWARDS 2 #define NET_MOVE_STRAFELEFT 4 #define NET_MOVE_STRAFERIGHT 8 #define NET_MOVE_JUMPJETS 16 DataStream[0] = NET_MOVE_BACKWARDS Big question is, whats the easiest way of reading out that bit again? The only way I can come up with is to start stepping though the data one bit at a time until I get to the bit I want, starting at the highest bit and subtracting values from the byte until I hit 0. Seems messy, is there a better way, something simple like: if (DataStream[0] = NET_MOVE_BACKWARDS) Humm... Why can''t I address bit like elements in an array? I can''t program in assembler BTW Many thanks Chris
Chris Brodie
Advertisement
you could try:

if (DataStream[0] & NET_MOVE_BACKWARDS)

...that should work
I usually use the ''&''- AND bitwise operator - when I want
to find whether a bit is on or off.
Hope that helps
Excellent!

I''ll give it a try, it was exactly the kind of direct testing I was looking for. Thanks...
Chris Brodie
You can also combine more than one command into that varible.

MOVE_UP 1
MOVE_DOWN 2
MOVE_LEFT 4
MOVE_RIGHT 8
MOVE_FIRE 16

myMove = MOVE_UP / MOVE_FIRE

Now I am moving up and firing at the same time.
That slash should be a pipe.

Stupid message board.
Here are a few #defines that I find useful:
#define SetBit(b,c) ((c) / (b))#define ClearBit(b,c) ((c) ^ (b))#define TestBit(b,c) (((c) & (b) == 0)) 

To use it:
...#define NET_MOVE_FORWARD      1#define NET_MOVE_BACKWARDS    2#define NET_MOVE_STRAFELEFT   4#define NET_MOVE_STRAFERIGHT  8#define NET_MOVE_JUMPJETS     16if( TestBit(DataStream[0],NET_MOVE_FORWARD){   //do something} 


Just my $.02 worth

David "Dak Lozar" Loeser

P.S.
I would also suggest using hex values for your defines
it makes it somewhat easier to read
eg.
#define NET_MOVE_FORWARD      0x01#define NET_MOVE_BACKWARDS    0x02#define NET_MOVE_STRAFELEFT   0x04#define NET_MOVE_STRAFERIGHT  0x08#define NET_MOVE_JUMPJETS     0x10#define NET_MOVE_HIDE         0x20 


Edited by - Dak Lozar on 4/26/00 11:18:11 PM
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
You''ll want to create a bitfield datatype class, it makes it easier to manage and manipulate bitfields. Also consider using another form of constants other than #defines, perhaps enumerations or consts, it''s safer imho. Good Luck!

-ddn
quote: Original post by Dak Lozar

Here are a few #defines that I find useful:
#define SetBit(b,c) ((c) / (b))#define ClearBit(b,c) ((c) ^ (b))#define TestBit(b,c) (((c) & (b) == 0)) 


Should that not be /= and ^=, respectively?
Warning: using ^= to clear a bit is a little hit-and-miss. If that bit was already cleared, you will end up setting it! This means you'd really have to check your bit beforehand before clearing it, eg:
if (!TestBit(variable, SOME_BIT))    ClearBit(variable, SOME_BIT);   

Which seems like too much hassle to me. Try changing ClearBit above to ToggleBit (a more accurate description) and use this instead:

#define ClearBit(b,c) ((b) &= (~c))

Which should preserve every bit in the original variable except the bit(s) you are trying to clear, with no conditional checking on your part.

Edited by - Kylotan on 4/27/00 5:44:06 AM
Kylotan,
Your correct... thanks for catching that.
Here's how it should look
#define SetBit(b,c) ((c) / (b))#define ToggleBit(b,c) ((c) ^ (b))#define ClearBit(b,c) ((b) &= (~c))#define TestBit(b,c) (((c) & (b) == 0)) 


When I tried to add the = (equal) sign to the macro as you suggested, I got complaints from VC++.... other than that these macros should work of course you could always create a bit class that held these as member funcs.

Btw, the point the Anon poster made about enums is valid.

I am a long time "C" programmer (about 14 years) that has been coding C++ for about 3 years. So, breaking my #define habbit is difficult. As of late I have been limiting them to utility macros as explained in the March 2000 issue of C/C++ Users Journal page 27.




David "Dak Lozar" Loeser

Edited by - Dak Lozar on 4/27/00 7:58:27 PM
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
Thank''s so very much everyone, I feel armed with heaps of cool techniques now to get this done.

From these cool macro''s through to the minimal bit data types I''m inventing as I go along.

Thanks anon poster too, I didn''t really the read the enum bit until Dak drew my attention to it (I''ve never used enum before) but I suppose it would be a pretty cool way of doing it.

I still don''t get the comment about bitfield datatype classes. Right now I''m just using an array of char''s and looking at something called a byte datatype. Is there something better I should use?

BTW : I really starting to get the hang of the lockstep method that brad pickering mentioned a while back. You should see the kind of things you can do with it. I can hardly wait to play my own game! I''ve just realised that with lockstep you can safely do shooting collision testing on you own machine and just post the results out if you actually hit. -very cool-

chris
Chris Brodie

This topic is closed to new replies.

Advertisement