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.

DarkBASIC Professional Discussion / both easier to rewrite this code?

Author
Message
Star Vitebsk
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: Belarus
Posted: 31st May 2013 15:30
flags = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)

[email protected]
icq: 157980031
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 31st May 2013 17:27
What's the question?

This is my current project, check it out! [href]forum.thegamecreators.com/?m=forum_view&t=204576&b=8[/href]
This is my website, check it out! [href]http:\\www.TeamDefiant.co.uk[/href]
Star Vitebsk
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: Belarus
Posted: 31st May 2013 17:34
how to write this on a different line, simpler

[email protected]
icq: 157980031
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 31st May 2013 18:59
easy.



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Jun 2013 01:16
That's simpler if you make more than one call. But of course it would be simpler still if you'd written



(removed redundant brackets).
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 1st Jun 2013 05:50
I never write anything with double bars like that, and I didn't care to pick it apart.

Star Vitebsk
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: Belarus
Posted: 1st Jun 2013 10:02
and that makes the team: || and the team <<
?

[email protected]
icq: 157980031
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 1st Jun 2013 10:48
|| is binary OR operation. The operations takes the two values on either side and merges them to create the answer. The merge occurs at bit level.


<< Shift Bits Left operation. For each shift left, we're multing the value by 2.

The original code is packing the four key states into a single value. So it reads each key state, which gives a 0 or 1 (On or Off) answer. It then is shifts (mults) the answers and by a power of 2. So each answer is represented by a single bit in the output.

To remove the OR and SHIFTS..

Original code,




Replace OR's with additions. This is viable since none of the shifted state bits overlap, if they didn, it wouldn't work.

Now: We've assuming here that SHIFT has precedence over ADDITION, so to avoid any unwanted behavior we stick some () around the calc's, just to be sure.




We can use Mults in place of the shifts. For each bit a value is shifted left is like multiplying by 2. So if we shift it 2 bits, thats like ((Value*2)*2) or Value*4. If we shift 3 bits left it's (((value*2)*2)*2) or Value *8.




This creates a 4 bit packed value of the 4 key states. This gives us 16 possible input combinations.


-----------------------------------------
Decimal / Binary / Keys Down
-----------------------------------------

00 =%0000 = No keys are pressed
01 =%0001 = UpKey
02 =%0010 = DownKey
03 =%0011 = DownKey + UpKey
04 =%0100 = LeftKey
05 =%0101 = LeftKey +UpKey
06 =%0110 = LeftKey +DownKey
07 =%0111 = LeftKey +DownKey+ UpKey
08 =%1000 = RightKey
09 =%1001 = RightKey + UpKey
10 =%1010 = RightKey + DownKey
11 =%1011 = RightKey + DownKey + UpKey
12 =%1100 = RightKey + LeftKey
13 =%1101 = RightKey + LeftKey +UpKey
14 =%1110 = RightKey + LeftKey +DownKey
15 =%1111 = RightKey + LeftKey +DownKey+ UpKey

Star Vitebsk
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: Belarus
Posted: 1st Jun 2013 10:51
thank you

[email protected]
icq: 157980031
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 1st Jun 2013 13:16
The bars are common in other programming languages. The << operator I've never used before. Honestly I didn't even take a look at it. Part of good coding habits involves avoiding the urge to cram too much onto one line.

If this was C Programming or a Micro Controller chip, and I was dealing with raw hardware inputs, then I'd be bit cramming like this. It makes sense reading controller input like this. But where the hardware is abstracted with DirectX... and resources astronomical by comparison, it's not that essential.

It's certainly elegant, but I just think you're making more work for yourself doing it this way.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Jun 2013 23:56
Quote: " Honestly I didn't even take a look at it. Part of good coding habits involves avoiding the urge to cram too much onto one line."

Another part of good coding habits is to look at it


I wouldn't consider this cramming really. It's a short line and looks clean. I've used bit shifting quite a few times in DB and AGK.

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 2nd Jun 2013 01:37
He only asked how to write it simpler.
Consider it revision 1.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Jun 2013 22:13
Indeed - and nobody has answered his question. Is addition simpler or faster than || ?

I prefer addition because I find it easier to read and would probably use that unless it was a time critical calculation in which case what should be used?

I guess there's only one way to find out ...

Login to post a reply

Server time is: 2026-07-07 05:40:46
Your offset time is: 2026-07-07 05:40:46