- signed int to binary?

This is a discussion on - signed int to binary? within the C++ Programming forums, part of the General Programming Boards category; I am very new to C++ coding and a ran accross a bump in the road. I bought a beginners ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    - signed int to binary?

    I am very new to C++ coding and a ran accross a bump in the road. I bought a beginners book called Beginning Visual C++ 6 by Ivor Horton. In the section about bit shiftting it explains how bit shifting works which I understand on a binary level. What I am having problems with is how to convert signed integers into binary. The book says that -104 as a signed int is represented by 1001 1000 in binary and -26 is represented by 1110 0110 but it does not explain how or why. I know how to convert between binary, HEX, OCT, and BCD from unsigned ints. Can anyone explain how to convert signed + and - ints into binary.
    Any help is appreciated
    Thank You.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Negative values are stored as "two's complement", it makes mathematics on the numbers very simple. To find the two's complement representation of a negative number, do the following:

    * Write the positive number in binary.
    * Invert all bits (0 becomes 1, 1 becomes 0). This is "one's complement"
    * Add 1 to get two's complement

    For example, -26 as 8 bits:

    * Write the positive number in binary.

    26 = 0001 1010

    * Invert all bits

    1110 0101

    * Add 1

    1110 0110

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Everything in a computer is binary. It just comes up to how you want to represent it. The unsigned int is already binary but when the computer outputs it to the screen, for example, it does so in decimal format (the one humans use). AFAIK C++ doesn't let you handle binary by yourself.
    I found this link: http://www.usbyte.com/common/Binary%...conversion.htm it has some links to a binary tutorial.
    I did a google search for "binary conversion" and that was the first link I found.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Negative values are stored as "two's complement"
    Not always. Signed integer representation in binary takes one of three forms:

    Ones complement: By inverting all of the bits of the corresponding positive value.
    Twos complement: By inverting all of the bits of the corresponding positive value and adding 1.
    Sign-magnitude: By setting a sign bit.
    My best code is written with the delete key.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yes, but I have yet to see a processor which uses anything other than 2's complement. 2's complement arithmetic is so much simpler to build in hardware, it's really the only representation that makes sense to use when you're designing hardware.

    The only ICs I've seen using sign/magnitude are some kinds of acquisition cards and the like, and I don't believe I've EVER seen one's complement used by any piece of hardware.

    Offset binary is also used, and is more common than sign/magnitude or one's complement. In general, offset binary is just 2s complement with the highest bit inverted.
    Last edited by Cat; 08-24-2003 at 03:50 PM.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    Thank you for all the help. I looked up all 4 methods and will keep them in mind. My computer uses two's compliment method.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    All computers will; it's only when you get into microcontrollers, analog to digital converters, and the like that you find things that aren't two's complement.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation Don't get bogged-down with the binary...

    KnowledgeHungry,

    This binary stuff probably belongs in a later chapter, or in an appendix. Horton seems to cause a lot of confusion by throwing it into chapter 2!

    I don't have the book, but it seems very condensed. I think it could be 3 books! (Beginning C++, Windows Programming, and Using MSVC++.)

    Binary is not used that much. (I use it "every day" because I work with hardware.) If you are going to be a professional programmier, or if you're going to get a Computer Science degree, you MUST understand binary and bitwise operators. But, you can do an awful lot of programming without it!

    One last point - Usually, when programmers work with binary, they will actually use hexadecimal in the program. You'll almost never see bitwise operators used with signed-decimal numbers. Usually, the binary number is being treated as a "bit-pattern" rather than a "number".

    But, as -=SoKrA=- said, everything is binary inside the computer, so the computer doesn't care if you consider it a number, or a bit-pattern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM

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