Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   A few simple programming questions; (http://www.chiefdelphi.com/forums/showthread.php?t=44028)

aaeamdar 15-02-2006 21:16

Re: A few simple programming questions;
 
Quote:

Originally Posted by Keith Watson
I've seen bugs introduced when people use pre or post ++ at the wrong time.

An example? I'm curious.

Quote:

Originally Posted by Keith Watson
Even something as simple as always using parentheses to establish precendence instead of relying on operator precendence makes code much more understandable.

Some people think that there is somehow an advantage to cramming something into the fewest lines possible (ahem!). Sometimes it's necessary to optimize things even if these optimizations make things less readable. If your program does billions of mod 32 calculations, and you have some method

Code:


int mod32(int x)
{
    while (x > 32)
    {
            x -= 32;
    }

    return x;
}

This works and everyone should be able to tell what it does. I think there's also a modulus method in the C libraries, but I could be wrong. Anyway, the point is that this would probably be a fairly clear way of doing it. But if you were trying to reduce the amount of time your program takes (and it could take a while if you enter in large numbers and do millions of calculations) this is what you would want the method to do:

Code:

int mod32(x)
{
    return x & 31;
}

It's less understandable. Some people may not even know what the & operator does. An italisized description follows:

The & is a bitwise AND operation. In other words, it works with the ones and the zeroes. AND evaluates to true or one if cond1 and cond2 evaluate to one. Example: 7&11

7 = 4+2+1;
11 = 8+2+1;

0111
1011
____
0011


The bitwise operation works faster. It only works with powers of two (don't try x&10) but in those cases, it works. If you want, think about why (or PM me). Just draw it out.

However, oftentimes the best code for processing time is the most understandable too. Also, when you think about the speed of today's processors, unless you are writing code that /needs/ to run fast, there is not much point in optimizing.

[\END LECTURE]

That was rather off-topic, but I found it interesting. I hope you did too (or skipped over it).

Tat's all for now, folks. Tit for tat.
Paul Dennis

Oumonkey 16-02-2006 18:01

Re: A few simple programming questions;
 
Wow, Thanks for all the help. Although that last post hurts my head. But now, I think I have everything working. We are hooking up electronics now so I haven't been able to test anything, yet. But still, I'd give you all hugs, If I could, and a cookie if I could!
Danke!!!
Team 1555's only poor little programmer.

AUWarEagle#1 17-02-2006 12:25

how to program a trigger
 
hi how are you, this is from team 1611. This is our second year in FIRST and i am new to programming. can you explain to me the process of programming the triggers? if it's possible, can you write one for me?? sorry for the inconvenience.

Greg Ross 17-02-2006 14:24

Re: how to program a trigger
 
Do you mean the triggers on joysticks? ("Trigger" also has another meaning in software development circles.) What do you want them to do? Once we know that, we should be able to help you. (Try searching too. I believe what you need may have already been covered in some other threads.)


All times are GMT -5. The time now is 20:59.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi