Thread: Changing from positive to negative numbers

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    well, *= is certainly an improvement upon the rube goldberg in the OP at least lol

    not sure if "0-x" is actually any simpler, but that's the only other thing i can think of.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    well, *= is certainly an improvement upon the rube goldberg in the OP at least lol

    not sure if "0-x" is actually any simpler, but that's the only other thing i can think of.
    No need for a constant either - a simple assignment, with no constants is all you need.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    x = -x;
    Last edited by robwhit; 08-08-2008 at 10:03 AM.

  4. #19
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Just saw this post and my quick thought was

    Code:
    cout << "-x = " << ~x+1 << '\n';
    Was that what you were trying to convey mats? Given x has been initialized with a signed int variable.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    Just saw this post and my quick thought was

    Code:
    cout << "-x = " << ~x+1 << '\n';
    Was that what you were trying to convey mats? Given x has been initialized with a signed int variable.
    No, robwhit gave the right answer: x = -x will negate an integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring negative numbers and moving on
    By quiet_forever in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 11:38 PM
  2. Question
    By Detrigan in forum C Programming
    Replies: 2
    Last Post: 10-19-2003, 10:13 PM
  3. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  4. Replies: 1
    Last Post: 10-01-2001, 10:39 AM