Browse > Home / C# / Make a positive number a negative number in 1 line of C# code - A code quiz!

| Subscribe via RSS

Make a positive number a negative number in 1 line of C# code - A code quiz!

July 16th, 2008 Posted in C#

Here are 4 methods, each one is meant to turn a positive number into a negative.

public int ReturnMinusVersion1(int i)
{
    return -i;
}

public int ReturnMinusVersion2(int i)
{
    return –i -i +i;
}

public int ReturnMinusVersion3(int i)
{
    return ~i + 1;
}

public int ReturnMinusVersion4(int i)
{
    return i * -1;
}

The questions are:

Which methods compile?

Which methods produce the correct result?

Explain why the working methods work.

Which is best and why?

Any other cool ways of making a positive number a negative? Have fun!

kick it on DotNetKicks.com

Share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • blogmarks
  • DotNetKicks
  • e-mail
  • Facebook
  • Google
  • Live
  • Ma.gnolia
  • NewsVine
  • Pownce
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

9 Responses to “Make a positive number a negative number in 1 line of C# code - A code quiz!”

  1. Steven Parry Says:

    Clever stuff. What the hell is that 3rd method doing?!


  2. Will Says:

    That’s the bitwise complement operator, which should be off limits for about 99.999% of all .NET programmers.


  3. Yogesh Says:

    Nice. :)

    The second one won’t compile without proper parenthesis.

    All are ok, first one being the simplest and most approached method. Although I found your bit reversal method cool.


  4. Will Says:

    What I mean by that is the vast majority of .net devs are completely unfamiliar with any bitwise operations other than logical and/or, and that’s only because of enums. Therefore, to keep your code as maintainable as possible, unless you absolutely need to you shouldn’t mess around with them. Bit manipulation should be left to the realm of hard core apps and low level/highly optimized code.


  5. Ron Says:

    All those methods simply alter the sign. Your specification said to make a positive number negative, not make a negative number positive. Following the spec, this is the best (and the only right) answer:

    public int ReturnMinusVersion5(int i)
    {
    return -Math.Abs( i );
    }


  6. Carey Says:

    >All those methods simply alter the sign.

    Surely to all intent and purpose the same thing? I am not clever enough to tell! :) I do admire your spec thumping though!


  7. MoMo Says:

    >The second one won’t compile without proper parenthesis.

    Compiles and works fine here.


  8. Jay R. Wren Says:

    Ron: nice catch!

    Carey: it says make a positive number, negative. it does NOT say to make a negative number positive. So a negative number should remain the same.

    I think all should work. The first is the best and most readable IMO. But I would question why you are writing a method to do this at all. Just use uniary - operator where you need it inline.

    I really don’t like the second one because it will overflow at int.MinValue/2 instead of int.MinValue


  9. Dave Says:

    Ron,

    One thing I like is a challenge. I hate being told there is only one way to do something, and I like bitwise operators, so…

    public int ReturnMinusVersion5(int i)
    {
    return (1 << 31 & i) == 0 ? (-1 ^ i) + 1 : i;
    }

    Works for me!


Leave a Reply