• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only $9.99! Coding help with bitfields? Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. 9 replies to this topic #1gimp Members - Reputation: 142 Like Likes Like Posted 25 April 2000 - 04:21 PM 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 Sponsor: #2Epoch Members - Reputation: 122 Like Likes Like Posted 25 April 2000 - 04:40 PM 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 #3gimp Members - Reputation: 142 Like Likes Like Posted 25 April 2000 - 09:20 PM Excellent! I''ll give it a try, it was exactly the kind of direct testing I was looking for. Thanks... #4Buster Members - Reputation: 100 Like Likes Like Posted 26 April 2000 - 06:15 AM 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. #5Buster Members - Reputation: 100 Like Likes Like Posted 26 April 2000 - 06:16 AM That slash should be a pipe. Stupid message board. #6Dak Lozar Members - Reputation: 122 Like Likes Like Posted 26 April 2000 - 05:11 PM Here are a few #defines that I find useful: #define SetBit(b,c) (© / (b))#define ClearBit(b,c) (© ^ (b))#define TestBit(b,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

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 26 April 2000 - 05:43 PM

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

#8Kylotan  Moderators   -  Reputation: 3107

Like
Likes
Like

Posted 26 April 2000 - 11:36 PM

quote:
Original post by Dak Lozar

Here are a few #defines that I find useful:
#define SetBit(b,c) (© / (b))#define ClearBit(b,c) (© ^ (b))#define TestBit(b,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

#9Dak Lozar  Members   -  Reputation: 122

Like
Likes
Like

Posted 27 April 2000 - 01:57 PM

Kylotan,
Your correct... thanks for catching that.
Here's how it should look
#define SetBit(b,c) (© / (b))#define ToggleBit(b,c) (© ^ (b))#define ClearBit(b,c) ((b) &= (~c))#define TestBit(b,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.

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

#10gimp  Members   -  Reputation: 142

Like
Likes
Like

Posted 27 April 2000 - 06:19 PM

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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS