Convert a long to negative?

This is a discussion on Convert a long to negative? within the C++ Programming forums, part of the General Programming Boards category; This is something really easy but my brain is struggling today. I have a long which has a value of ...

  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
    22,114
    Yeah, like:
    Code:
    lValue = -lValue;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:47 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. hton long long
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 06-01-2008, 09:26 PM
  5. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM

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