# Thread: - signed int to binary?

1. ## - 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. 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. 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.

4. >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.

5. 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.

6. 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. 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. ## 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