Are negative signed integers, converted to binary, simply regular integers +256?

152 Views Asked by At

Are negative signed integers, converted to binary, simply regular negative integers +256?

00001010 = + 10 signed
00001010 = + 10 unsigned

11110110 = - 10 signed
11110110 = +246 unsigned (-10+256=246)

Notice how -10 (signed) is just -10+256=246 (unsigned)?

So in other words: If you want to represent a negative integer (such as -10) as a signed byte, you would simply add 256 before doing the base2 conversion?

I also heard that it can be done by "inverting all the bits and then adding one to the result".


PS: If it's not obvious, dealing with numbers isn't a strength of mine. I know that's probably not the case for most of you, but I'm trying to simplify the concept. Please keep that in mind, and if possible (respectfully) try to resist the urge to make it more confusing or complicated.

1

There are 1 best solutions below

4
On

That works for $8-$bit signed integers on a twos' complement machine. General-purpose computers all use twos' complement these days, but there are programmable logic arrays that use sign-magnitude. If you are using a high-level language like C, don't do this yourself, but let the compiler take care of it, because it will work differently for different-sized integer types. If you are using a language with unlimited sized integers, like python, this system doesn't work at all.