In my process of C code optimization*/
return (curr < 0 ? : ((curr > 255) ? 255 : curr));
/* I want the int value to be clipped between 0 to 255.
Is there any other way to optimize above code snippet? */
@Banfa: That will wrap a value like 256 to 0 where I beleve the op wants to have it be 255 for all values over 255.
@tinstaafl: Why require cmath for specific versions of min/max which have an already templated form?
Building from tinstaafls approach:
val = std::min ( 255, std::max (0, val) );
As an aside, it really doesn't make much sense to try and 'optimize' such statements. Small expressions are - in almost all cases - best handled by the optimization steps of the compiler. Things you do in source have very limited effect on the generated machine code.
Aim for readability; optimize only out of necessity.
Hard for it to be optimal when it doesn't do what you want. Both methods, using fmin/fmax and using MIN/MAX will do what you want. But as was pointed out anything you do to optimize something that's relatively straight forward, will get re-optimized by the compiler. So readability becomes more important in this case.
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...