![]() |
One line power
I got the insane urge to eliminate if statements. Having done that, i had the urge to eliminate statements in general. The result? One line of code per track to make it flip and reverse, rescale the sticks, and ramp the speed to reduce motor wear and tear.
creepbit =creep.bit7 creep = abs(127-creep) hold = lt lt = (254-rt -lt *creepbit + lt)*creep/127-creep+127 min (lth-Ramp_Speed) max (lth+Ramp_Speed) rt = (254-rt -hold *creepbit + rt)*creep/127-creep+127 min (rth-Ramp_Speed) max (rth+Ramp_Speed) rth = rt max (254-Ramp_Speed-1) min (Ramp_Speed+1) lth = lt max (254-Ramp_Speed-1) min (Ramp_Speed+1) sure, it has some maintainance code before hand, but it works. Creep is usually a wheel input. If it is full forward everything works normally. Full reverse will flip and reverse the tracks. Anything inbetween will rescale the tracks and possibly flip and reverse them too. Lastly, it will prvent motor burn. I got bored. So sue me |
Nice. If I can get my hands on a copy of our optimized code, I'll post it. However, we are not running with the optimized code because the two members of our team over optimzied it to be compleatly unreadable. I got lost as they tried to explain it to me.
Yours I can follow. Theres was optimized with bitshifts, which I still don't understand... |
I posted something about this subject a while ago and got no response. I'm a big fan of one line code instead of if statements, just like you. Not only it looks nicer, it's a very strong intellectual exercise. However, my doubt is: Which one is really better? One line code obviously takes less RAM space, but which is faster? Is there any other aspect to be considered?
(Our code contains only two if statements, one of them being on the 'DO NOT CHANGE' part of the code. Looks great, works great, and I'm willing to post/e-mail the code to anyone interested.:) ) |
if i knew more PBASIC, it would make more sense. also, if knew what each variable was. :D otherwise, it seems to make logical sense.
|
Oh man, I'm glad to hear I'm not alone! I love to make things into one liners, and then optimize them with bitwise operators. Last year I took a whole "paragraph" of code and put it into *one* statement ... let's just say it turned into a "paragraph" line :D
Hmm ... If statements definitely take longer, I read that somewhere and it makes sense. |
quick question, what is a bitwise operator? if someone can quickly explain it (i know a bunch of C++, so if someone can explain it in C++ terms that would be cool)? i belive the default code used nitwise operators to initialize the uP when power goes on, but i didn't understand what they did. also, what does "min" or "max" mean?
|
Bitwise operator
In C the common ones are bitwise OR | bitwise AND & and (please correct me if I'm wrong, my C book is at work) bitwise XOR ^
What the operators do is take the number in binary and perform the operation (or, and, xor) on the corresponding bits. OR evaluates to 1 if either input is 1, 0 otherwise AND evaluates to 1 if both inputs are 1, 0 otherwise XOR evaluates to 1 if only 1 (not both) inputs are 1, 0 otherwise Example: 148 | 51 -> 10010100 | 00110011 = 10110111 -> 183 148 & 51 -> 10010100 | 00110011 = 00010000 -> 16 148 ^ 51 -> 10010100 | 00110011 = 10100111 -> 167 If you take an intro to computer engineering class in college, you'll learn this within the first couple of weeks. If you have more questions ask away, we're here to help. Mike |
MIN and MAX
Quote:
Other languages have MIN and MAX functions which compare two (or more) operands, and return the smallest (or largest) value. In PBASIC, the operation is actually exactly the opposite! MIN returns the larger of two values, and MAX returns the smaller! But it isn't really that confusing. Just think of it as "clamping" the minimum and/or maximum values of a variable or calculation. For example, X = X MIN 10 will leave X unchanged as long as it is greater than (or equal to) 10. However, if its value drops below 10, it is set to 10 instead. MAX works similarly. BUT BE CAREFUL! MIN 0 DOESN'T DO ANYTHING! You might think that it would prevent the result of a calculation from going negative, but you'd be wrong! MIN and MAX only work with positive numbers, and if you try to give them negative numbers, they will look at them as very large positive numbers! (The reason for this is that the BS2 stores negative numbers in twos complement form. See this message for a very concise description of how twos complements work.) |
Although one liners are nice, they're not necessarily faster. Multiplication instructions are always SLOW. Once you get to the point where the lines of code are that horribly long (and having non-bitwise operators), I'd question the speed/code size improvements, if any.
In response to the comment about eliminating 'if' statements for speed: you're right and wrong. With today's modern processors you generally want to avoid using 'if' statements as much as possible due to branch prediction. However, I don't believe the simple processor on the BASIC STAMP implements pipelining. You'd probably be taking more cycles, and probably more memory as well, by doing all of that math than if you simply used 'if' statements. My favorite line from my optimized code is: Dr_Motor3 = (40 << (~(basket_lift | basket_lower)) << 2) | (15 >> (~(basket_lift) << 2)) It effectively eliminated a 4 condition branch statement ;-) PBASIC was so horrible that I *wanted* to program in assembly. |
wow, and i thought C++ was somewhat complicated sometimes. :D i wish we could program in something a bit higher level, but i guess Java and C++ are more application only code. oh well, i'm planning on going into computer science, or something relatred to that, in college, guess it doesn't hurt to know more code. :D
|
just looking at those one liners makes me cringe...
debuggin one liners definitely takes a lot of thinking on your part, though they have got to make your files a lot smaller :) (less hd space = good) |
Quote:
|
you get that down around 10% and i then i might be impressed
:D |
C++ bitwise ops?
With all this talk of bitwise vs non bitwise i got thinking -- Can I apply this to C++? so far i know that & and ^ are and and or, but it would be helpful to know more. If anyone knows how to do this i would be much obliged.
|
Re: C++ bitwise ops?
Quote:
|
| All times are GMT -5. The time now is 11:58. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi