Thread: Convert a long to negative?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Convert a long to negative?

    This is something really easy but my brain is struggling today. I have a long which has a value of 10. I want to convert it to negative (i.e. -10)

    I am using

    lValue = lValue - (lValue * 2);

    There has to be a simpler bit swapping way of doing it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, like:
    Code:
    lValue = -lValue;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    See, I said my brain had given up for the day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]unsigned number with negative value?
    By learn in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 12:00 AM
  2. Convert bytes to int, keep the negative number
    By umm in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 04:47 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. hton long long
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 06-01-2008, 08:26 PM
  5. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM