Thread: Assigning Binary Value to a Variable

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Talking Assigning Binary Value to a Variable

    Hi All:

    I'd like to know how to assign a 2's compliment binary number to a variable (sorry I totally forgot how to do that)

    For instance, I'd like to assign the number 5

    5 in Binary is: 000101
    5 in 2's Comp is: 111011

    Can some please help me with this?

    Thanks a lot

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You mean -5? Use unary - on a 2's complement machine. Or use ~ and add 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Convert the number to hexadecimal and assign it that way. (Or octal.)

    You assign a hex number like this:
    Code:
    int x = 0xab12;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Assigning an object variable to a constant reference
    By Canadian0469 in forum C++ Programming
    Replies: 18
    Last Post: 11-10-2008, 10:48 AM
  3. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM