views:

282

answers:

10

Currently, my algorithm is someValue + x. I am trying to figure out how I can transform x to 1 if its greater than 0, or to 0 if its equal to 0.

I don't want to use an if-else to produce this, but rather just transform x in my algorithm by itself.

For example: If someValue = 20, x = 4. It would produce 20 + 1 = 21. But if x = 0. It would just produce 20.

Thanks.

+7  A: 

Use

someValue + (x>0? 1: 0);

But don't worry about writing a little to get what you need. It's important to write understandable code :)

helios
+1  A: 

return x > 0 ? someValue + 1 : someValue;

but it ends up with a conditional statement anyway. You just dont do it explicitly.

Katalonis
+2  A: 

Assuming x will always be >=0 you can do:

x = (x > 0) ? 1 : 0;
codaddict
+3  A: 

Its simple operation , you can just use this

 n + (x>0? 1 : 0);
org.life.java
+15  A: 

Why you want to do this without an if statement may be questionable but (as of Java 5) Integer has a signum method which will return -1, 0 or 1 depending on the integer value:

int newVal = someValue + Integer.signum (x);

Assuming your input value will be always zero or more, this will do what you want. But it's unlikely to be any faster than an if statement like:

int newVal = someValue;
if (x > 0) newVal++;

nor any more succinct than the ternary version:

int newVal = (x > 0) ? someValue + 1 : someValue;

I just though I'd include it as an option in case you want neither the if nor ternary variant.

paxdiablo
+1 for using `Integer.signum`
oksayt
Well, it answers the question but I'm not sure it's actually "better" than any other solutions. Still, it's an option and it's useful to know about so do with it what you will.
paxdiablo
+1 for using `if`
Kobi
+1 because it answers the gist of the OP's question.
Tony Ennis
A: 

Just to throw something else in the mix that could work (but not necessarily a sensible way to go about it)...

int value = 0;
try {
    value = x / x;
}
catch (Exception e) { }
return someValue + value;
Richard Fawcett
The idea is interesting but throwing exception will lead to really poor performance.
Elenaher
I thought it might. I take it it would be much more inefficient than an if/else?
Richard Fawcett
The OP's question is non-sensical. Who cares if the performance is poor?
Tony Ennis
Ah, I was about to post a similar solution. If this is an intellectual exercise, this is a good answer. If this is a real programming problem, the correct answer is to use an IF -- the try/catch is far slower and less obvious. The requirement that it be done without using an IF ... well, I just would ask "Why?"
Jay
+1  A: 

For readability, define an extra method for the conditional part:

 public static int absoluteSignum(int x) {
   return x == 0 ? 0 : 1;
 }

and use it in you code:

 int result = someValue + absluteSignum(x);
Andreas_D
+2  A: 

Using someValue+(x>0)?1:0 should at best be equivalent to:

if(x>0) someValue++;

when compiled, so the branching is not avoided. If you for some reason absolutely have to avoid branching you can use:

x=-((~(x-1)|x)>>31);

As a bonus, for C language this would be:

x=(~(x-1)|x)>>(sizeof(x)*CHAR_BIT-1)

The result is 0 if x==0, 1 otherwise. It's pure bitwise ops, no branches and total 5 integer operations. For similar tricks (although this one isn't listed, I just modified another algorithm there) you can see this bit twiddling hacks page: http://graphics.stanford.edu/~seander/bithacks.html

jjrv
CHAR_BIT? You did _see_ the Java tag, yes? :-)
paxdiablo
Oh dear, now switched it to Java and left the C version because it was so nice :)
jjrv
In C, just do `someValue + !!x`
nos
+1  A: 
x=-((~(x-1)|x)>>31);

in my opinion,it's the best answer.But first you should store the X into a 32-bits data type. such as int (x86).
No branches,No judgment

shenju
No readability...
Carlos Heuberger
@Carlos Heuberger - That's not what OP wanted. No branches, I like it.
Ishtar
No readability?
shenju
what's the most important of algorithm?
shenju
readability?No,it's the performance!
shenju
Neither readability or performance is important. The OP's question is non-sensical and is little more than a fun test.
Tony Ennis
I'm not that sure that `if (x > 0) y++` has worse performance than `y=-((~(x-1)|x)>>31)`... also OP only wrote that he wanted to avoid IF-ELSE, nothing about branches nor about performance, maybe he just wants to improve readability. At my work for sure readability is much more important than performance, at least concerning micro-optimizations.
Carlos Heuberger
+2  A: 

If x is non-negative integer:

return someValue + (x + 2)%(x + 1);
Vadim Lobanov