Changing from positive to negative numbers

This is a discussion on Changing from positive to negative numbers within the C++ Programming forums, part of the General Programming Boards category; well, *= is certainly an improvement upon the rube goldberg in the OP at least lol not sure if "0-x" ...

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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 11: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.

Page 2 of 2 FirstFirst 12
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-30-2008, 12:38 AM
  2. Question
    By Detrigan in forum C Programming
    Replies: 2
    Last Post: 10-19-2003, 11:13 PM
  3. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 01:48 AM
  4. Replies: 1
    Last Post: 10-01-2001, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21