arithmetic help

Printable View

• 09-18-2007
freeindy
arithmetic help
Hi,
I want to make a programming procedure a bit more compact if possible:

I have the following code:
Code:

```available = QUEUE_SIZE - sendQ.count; if(available < length)         copySize = available; else         copySize = length;```
but want to reduce to a singal line:
Code:

`copySize = ???`
All I want is that the variable copySize always land a value between 0 and (QUEUE_SIZE - sendQ.count).

is it possible?

Thanks,
Indy
• 09-18-2007
mike_g
I think this might do it:

copySize = (available < length)? available : length;
• 09-18-2007
matsp
Quote:

Originally Posted by freeindy
Hi,
I want to make a programming procedure a bit more compact if possible:

I have the following code:
Code:

```available = QUEUE_SIZE - sendQ.count; if(available < length)         copySize = available; else         copySize = length;```
but want to reduce to a singal line:
Code:

`copySize = ???`
is it possible?

Thanks,
Indy

you mean something like:
Code:

`copySize = (available < length)?available:length;`
This will make it more compact, but only in number of lines. The code generate by the compiler will be exactly identical. The sampe applies if you inline the calculation of
Code:

`available = QUEUE_SIZE - sendQ.count;`
to:
Code:

`copySize = (QUEUE_SIZE - sendQ.count < length)?QUEUE_SIZE - sendQ.count:length;`
All you've achieved now is to make the code less legible - and if you are REALLY UNLUCKY, the compiler will do the subtraction to calculate the "available" twice, which makes the code slightly slower. Unlikely, but possible.

I think the original code looks just fine.

--
Mats
• 09-18-2007
freeindy
ah yeah ofcourse... i was sitting and trying with &#37; + / *.
thanks mike_g
• 09-18-2007
matsp
Your clarification doesn't change anything, you still need to check if "available" is larger than length, in which case you should only copy length - or isn't that the case in your code?

--
Mats
• 09-18-2007
Salem
> I want to make a programming procedure a bit more compact if possible:
Whilst you can do this, the readability and maintainability of the result suffers as a result.

matsp is right, there is nothing wrong with the original.
• 09-18-2007
matsp
As I explained, it makes it a few lines shorter to use a conditional assignment, but the compiler should come up with exactly the same thing.

You could use % to make sure the size is OK, but it's not going to make it faster, even if it's shorter.

Making code "obvious" is a good goal. If you don't want to do that, then fine, but if it's not improving anything and making the code less readable, then I don't see the point at all.

--
Mats
• 09-18-2007
freeindy
i know about the readability/maintainability. the first reason was to use only arithmetic operation is for faster execution on the target machine which is not a pc. It's an atmel processor with a speed of 4 MHz i think. I thought it would be good to use faster execution method. But you're right. I should leave it to at it is.

Thanks, Indy.
• 09-18-2007
mike_g
I always thought obfuscation was one of the best things about C. When no-one else understands it you look clever :P
• 09-18-2007
matsp
Quote:

Originally Posted by mike_g
I always thought obfuscation was one of the best things about C. When no-one else understands it you look clever :P

I know this is supposed to be a joke, but: Yeah, right, except when you have to spend half your day every day explaining what the code that you wrote does, rather than writing new code.... The above principle is fine if you are the only one writing the code, but not if someone else is supposed to use the code (or you need to go back and fix bugs two years later when a customer with a special use-case needs something fixed that was never tested in the initial release).

--
Mats
• 09-18-2007
mike_g
Yeah, its just a joke dude ;)