24

So I'm just curious if there is a short hand statement to this:

if(number < 0 )
  bigInt.sign = 0;
else
  bigInt.sign = 1;

I see all these short hand statements for if a < b and such.

I'm not sure on how to do it properly and would like some input on this.

Thanks!

I actually just figured it out right before you guys had answered.

The shortest solution is bigInt.sign = (number < 0) ? 0 : 1

Ralf Ulrich
  • 1,575
  • 9
  • 25
kevorski
  • 816
  • 1
  • 11
  • 29
  • possible duplicate of [Ternary operator ?: vs if...else](http://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else) – πάντα ῥεῖ Jul 17 '14 at 02:33
  • Thanks to all who answered this. I appreciate it, this is really the first time that I have used the shorthand if-else statement, so I am just trying to get used to it – kevorski Jul 17 '14 at 02:35
  • 2
    @kevorski `bigInt.sign = (number < 0) ? 1 : 0` - this is the opposite of your original `if`/`else` code, but otherwise good. – Tony Delroy Jul 17 '14 at 02:50
  • @πάνταῥεῖ not quite, that's more performance related. Although I'm sure there's a duplicate for this somewhere. – OMGtechy Aug 02 '14 at 03:36

5 Answers5

63

The basic syntax for using ternary operator is like this:

(condition) ? (if_true) : (if_false)

For you case it is like this:

number < 0 ? bigInt.sign = 0 : bigInt.sign = 1;
Bla...
  • 7,228
  • 7
  • 27
  • 46
25

try this:

bigInt.sign = number < 0 ? 0 : 1
  • What, omitting whitespace makes it "shorthand?" – Matt Ball Jul 17 '14 at 02:30
  • 2
    Just a stylistic choice so no downvote from me, but IMHO this is not the cleanest way to use the conditional operator here... `bigInt.sign = number < 0 ? 0 : 1` is better factored, and emphasises `?`'s use to select between values rather than using it for flow control. – Tony Delroy Jul 17 '14 at 02:54
  • 1
    You can also replace `?` and `:` with `&&` and `||` respectively. – Krii Feb 25 '16 at 03:47
18

Yes:

bigInt.sign = !(number < 0);

The ! operator always evaluates to true or false. When converted to int, these become 1 and 0 respectively.

Of course this is equivalent to:

bigInt.sign = (number >= 0);

Here the parentheses are redundant but I add them for clarity. All of the comparison and relational operator evaluate to true or false.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Tbh, I'd just do `number >= 0`, but I'd put in a `static_cast` to make it clear a 0 or 1 is desired. – chris Jul 17 '14 at 02:31
  • @chris this is very basic c++ program that i'm making for school. just learning c++ right now. – kevorski Jul 17 '14 at 02:33
  • 2
    @MattMcNabb, It's redundant, but the cast makes it clear that you didn't accidentally expect it to be stored in a `bool` or anything silly like that. I'd hazard putting in the literal type of `bigInt` for clarity on that. Implicit `bool` to `int` conversions kind of suck in general imo. – chris Jul 17 '14 at 02:35
  • +1, but one minor nitpick: I'd use `bigInt.sign = (0 <= number);` so that the ordering reads left-to-right. This becomes more attractive if you have multiple comparisons such `0 <= number && number < SomeUpperBound` – TemplateRex Jul 17 '14 at 06:38
0

Depending on how often you use this in your code you could consider the following:

macro

#define SIGN(x) ( (x) >= 0 )

Inline function

inline int sign(int x)
{
    return x >= 0;
}

Then you would just go:

bigInt.sign = sign(number); 
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0

you can also try this :

bigInt.sign = (number<0)*0 + (number>=0)*1;

If we need to assign another value other than 0 and 1 then this code can be used like :

 bigInt.sign = (number<0)*(replacement_of_0) + (number>=0)*(replacement_of_1);
UNREAL
  • 430
  • 5
  • 11