# 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. 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

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?

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

*Originally posted by Ian W. *
**also, what does “min” or “max” mean? **

In PBASIC, MIN and MAX are somewhat counterintuitive. (That means they work differently than you might expect. )

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:
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. 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.

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)

*Originally posted by ChrisA *
**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) **

We arn’t running the optimized code for excatly that reason. To hard to debug. But it does only use 18% of the memory. Small enough for ya?

you get that down around 10% and i then i might be impressed

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.

*Originally posted by VanWEric *
**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. **

C++ has all the same bitwise operators: & does a bitwise AND, | is the bitwise OR, and ^ the bitwise XOR. (I think you misspoke when you said ^ is OR.) << and >> are the bitwise shift operators, and ~ is the unary bitwise negation.

heres a rule of thumb for ya…
if mike soukup replies to your post with code in it, it will usually be in either c or c++. (thats bout all he normally codes in).
(you know its true mike )

so actually your question was answered before you even asked it even though it was hidden from view.

I know this is a little off topic, but in PBASIC i can directly edit the bits of a byte – my_variable.bit7 directly mucks around with bit 7 of that variable. Can i do this in c++? most of the code i write in pbasic gets a speed boost from mucking with it that way. Sorry about the tangent.

In C or C++ you can do it “the hard way”
my_var & 2^bit_wanted (assuming bit0 is least significant bit)
note: above ^ is not XOR but is 2 raised to some power
but that isn’t an lvalue, so you can’t “directly muck around with it.”

Another option is to use bitfields. I haven’t done that in a while, and I don’t have a book infront of me, so I won’t try to post an example. If I remember correctly, though, you aren’t able to specify which bit number each variable refers to, but you can mimmick the effect if you don’t rely on bit7 to be the 7th bit.

Stephen