Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Explain please flags = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)

Author
Message
Paulmac
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 12th May 2010 20:06
flags = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)


I am using the multiplayer example as a basic start for my multiplayer game and adding additional flags, Can anyone explain this command, Im a bit hazy on what || (2 vertical pipes) mean, am i right in thinking it is concatenate ? or does it have some other meaning in this context. also what is the << 1 doing ? From my old unix days this used to mean take input from keyboard but I think Im now going off at tangents.

I am trying to add another flag which would be inkey$() so for example if the shift key was held down then inkey$() = 54.

thanks in advance
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 12th May 2010 21:10 Edited at: 12th May 2010 21:15
Hi Paulmac. As I understand it, the double pipe is a bitwise OR statement which will merge or "add" the binary numbers of the two operands, so the code is saying:



The << operator is a bitshift operator which shifts the binary value to the left by the operand. In the first case:

downkey() << 1

This is saying the value returned by the downkey() function then bitshifted 1 place to the left. downkey() will only ever return a 0 or 1, and 0 bitshifted any number of places will always be zero, but 1 bitshifted by 1 would be 2.

if leftkey() returns a 1, it would be bitshifted 2 places which would mean the value is 4.

if rightkey() returns a 1, it would be bitshifted 3 places which would mean the value is 8.

Basically, it looks like the author is creating a single unique code that tells them which of the 4 keys are currently depressed. For example, if the only the downkey was pressed (0 || 2 || 0 || 0), the flags value would be 2. If the downkey and the leftkey were pressed (0 || 2 || 4 || 0), the value of flags would be 6. If all keys were pressed, the value of flags would be 15. Hope this makes sense.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 12th May 2010 22:24
roswell1329

You nailed that sucker, on the head. You may be a DBPro Newcomer, but it’s obvious you have been around the programming block.

Well Done.
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 12th May 2010 23:51
Thanks, bndelano. Glad I got it right!
Paulmac
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 13th May 2010 13:16
Thanks Roswell, thats made it clearer.
Ive now been able to add a 5th bit to the result so I can now check if the shift key for example is held down at the same time as the other keys. this allows my object to run or walk and transmit whether he is running or walking to the rest of the network players thus changing the object frame to a run or walk frame on the other players monitors.
TYVM.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 11th Jul 2010 03:12
Wow, this code is too cool.

Being a novice hobbiest with DarkBASIC Pro myself, could someone tell me what sort of complications or limitations such a code has, and perhaps compare it to an alternate system that carries out a similar task?
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 12th Jul 2010 07:35
Well, using this method makes interpreting the information a little more complicated, but this method is great for multiplayer situations because you can store 32 different flags inside one DWORD variable. Sending a measly four bytes across a network is much more efficient than sending a different variable for each flag. The same goes for passing data to a function. I'd rather have one parameter that I could pick apart than have some ridiculous, 32 argument function definition lying around.

There really isn't a downside to using bits to store boolean information. The only thing someone could really complain about would be the interpretation, but it's not hard at all. You'll just have to mask everything else.

To get to the 5th bit, we'll use a mask like so:


That will isolate the 5th bit by clearing out all others, so we can test it like this:


Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 12th Jul 2010 14:26 Edited at: 12th Jul 2010 14:26
Quote: "(1<<5)"


You can also use %10000.

You can avoid those interpretation problems using constants. Something along the lines of:



Cheers!
Sven B

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Jul 2010 16:17
Really good info! Thanks to all.


Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 13th Jul 2010 03:29
Needless to say, I'm now learning about bitwise operators.! Woo-hoo, a new chapter is opening in my DBpro journey!
Aralox
17
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 13th Jul 2010 16:14
man this thread is good! im with burning feet man on this one, a whole world of bits and manipulation of them has opened up to me. thanks guys

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 14th Jul 2010 07:54 Edited at: 15th Jul 2010 14:35
More handy information can be found here;

http://en.wikipedia.org/wiki/Bitwise_operation

It takes a little bit to get your head around the concepts, but eventually all starts to make sense sooner than later. But unfortunately I can't find too many DarkBASIC Pro examples of binary/bitwise operations; if anyone else has some simple & useful examples or tutorials, by all means, please post them.

Currently, I'm starting off with the small step of showing a variable as a binary byte... but can't get it to work.



EDIT: Oh oh, halfway there!



That kinda works! ARK, GETTING OVER STIMULATED. CODING IS AWESOME!

DOUBLE EDIT:

Ok, now I'm stuck. My code is as per below.



Any ideas?

ZOMG IT'S A TRIPLE EDIT!

I did some reading last night and this morning. I decided to focus on what exactly a bit is, and how it plays with bytes & words. May have solved my complication.




Now, what to do with my newly found binary powers!

Mer-mer-mer-MULTI-EDIT!

Ok, this one's done and dusted to me. Time to move onto increasing my logic skills.

Login to post a reply

Server time is: 2024-09-28 20:27:22
Your offset time is: 2024-09-28 20:27:22