/* Hi,
   In my process of C code optimization*/

    Clip(int Val)
        {
            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? */

I assume you are missing a 0 between the first ? :.

You could use the modulous (%) operator rather than a second test. Whether this is better rather depends on the processor it is running on though

Clip(int Val)
{
    return ((curr < 0) ? 0 : (curr %= 256));
}

Not sure about optimal but here's a way using included functions, rather than chained conditionals:

#include <iostream>
#include <cmath>
using namespace std;
int Clip(int Val)
{
    return fmin(fmax(Val,0.0),255.0);
}
int main()
{
    int test;
    test = 100;
    cout << Clip(test);
    return 0;
}

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

@L7Sqr - Doh! you're right of course.

@L7Sqr & @tinstaafl - Both your solutions use C++ but this is the C forum and in fact the original solution is valid on in C (it uses an implied return type).

I am beginning to suspect the original solution may well be the best C solution.

It's too late to edit, but I just realized that I placed C++ code here. The C solution is not much different:

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

val = MIN (255, MAX (0, val) );

N.B. The above expands to almost exactly what you provide originally but is much easier to read (and maintain).

You could also use fmin/fmax as suggested above but that would include converting between types.

oops changed the functions and didn't change the rest of the code.

Here's a C snippet for whatever it's worth:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Clip(int Val)
{
    return fmin(fmax(Val, 0.0), 255.0);
}
int main()
{

    printf("%d",Clip(300));
    return 0;
}

@All,
I want A Integer to be clipped between 0 to 255 i.e., If Val is negative make it 0 or if value is > 255 make it 255. When I searched through other materials i got the below sloution,

Clip(Int Val)
{
    if(Val & (~0xFF))
        return (-Val) >> 31;
    else
        return Val;
}

I guess is this one of the optimal sloution.

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.

int Clip(int Val)
{
    return (Val < 0 ? 0 : (Val > 0xFF ? 0xFF : Val & 0xFF));
}

My simple contribution.

commented: Just what I was going to post! :-) +12

This Val & 0xFF is an unecessary operator since you already know at this point that 0 < Val < 0xFF

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.