user437777
user437777

Reputation: 1519

What is most optimal way to replace integer '0' with '1' in c++

I have a piece of code where I multiply two numbers but condition is none of those integer numbers should be 0. If it is 0, I need to make it to 1 so that it is not counted.

y=x*z // x>0, z>0. if x==0 then x=1, if z==0, then z=1;

I want to avoid "if" condition check on every variable and replace with 1. Is there a better way of doing it.

Upvotes: 5

Views: 2758

Answers (9)

ST3
ST3

Reputation: 8946

Well, I used link from this answer and created such code. assembly here, assembly is shorter, so might be faster, but needs profiling, because shorter assembly might not be faster.

int main(int i, char**g)
{
  int x = *g[0];
  int z = *g[1];
  int tmp = z ? 1 : z;
  return (x==0?tmp:x*tmp);
}

Upvotes: 0

sjsam
sjsam

Reputation: 21965

Below

y=(x==0?1:x)*(z==0?1:z)

will give you this [ assembly ] code.


and an adaption borrowed from @Jerry Coffin's comment to his [ answer ]

y=(x+(x==0))*(z+(z==0)); 

will give you this [ assembly ] code.

Upvotes: 15

Twinkle
Twinkle

Reputation: 524

It will work

y=(x?x:1)*(z?z:1)

Upvotes: 1

Mostafa Vatanpour
Mostafa Vatanpour

Reputation: 1408

y=max(x,1)*max(z,1);

Define max function if necessary by #define preprocessor keyword or as a normal function.

Upvotes: 2

MaksG
MaksG

Reputation: 30

If you're concerned about precise results, then you should use the if statement you have in your comment or the ternary operator ? as most people are suggesting.
If you simply care that there be no branches in this section of code you can always OR 1:

x = x | 1;
z = z | 1;
y = x * z;

This would guaranty that there would be no branches in this code and that neither x or z are every equal to 0. But the price to pay is precision, since you could always accidentally add 1 to either x or z:

00000000 | 1 = 1
10001011 | 1 = 10001011
10101010 | 1 = 10101011 <- imprecision introduced!

Upvotes: -2

Jerry Coffin
Jerry Coffin

Reputation: 490408

I'm certainly not going to guarantee that it'll have any positive effects on your code, but one possibility would be something like this:

x += int(x == 0);
z += int(z == 0);

This compares the variable to 0, producing false or true. That's then converted to an int, giving 0 if the value was previously non-zero, and 1 if it was zero. We then add that result to the variable, so if it was zero, we'll add one, giving 1. If it was any non-zero value, we add zero to it, which obviously doesn't affect its value.

Upvotes: 3

Benjamin Lindley
Benjamin Lindley

Reputation: 103733

y = (!x ^ x) * (!z ^ z);

This works because when you xor a number with 0, you get the same number back. So if x != 0, then !x == 0, and !x ^ x is equivalent to 0 ^ x, which is x. And if x == 0, then !x == 1, and !x ^ x is equivalent to 1 ^ 0, which is 1.

Upvotes: 11

zneak
zneak

Reputation: 138181

I honestly doubt that you could come up with a way that would be significantly underperforming. Pick something that you can read later instead of something that you believe will be the fastest. Chances are that this specific operation won't be a bottleneck.

Upvotes: 6

LibertyPaul
LibertyPaul

Reputation: 1167

If one of them is 0 then addition will give you same result as if you change 0 to 1

y = (x * z > 0 ? x * z : x + z)

Upvotes: 1

Related Questions