For the purposes of teaching, I wish to understand the following in the context of the method (four steps, see below): I am trying to convert +7 to twos complement in 8 bits. The answer, as per shown on https://www.allmath.com/twos-complement.php is as below:
Decimal 7 Binary 0000 0111 Complement 1111 1001
This shows a negative number as it starts with 1.
Shouldn't the answer be 0111.
I want to be able to derive the answer using these four steps.
STEP 1: Convert magnitude (7) to binary. 0111 STEP 2: Pad 0's to desired bit size. 0000 0111 STEP 3: Invert bits to achieve 1's-complement. 1 1 1 1 1 0 0 0 STEP 4: Add 1 to achieve 2's-complement.
ADD 1 ------------------> 1 1 1 1 1 0 0 1
Where am I going wrong in understanding this. When working with twos complement numbers we were taught that if it started with a 1 it was negative. So what is a method for converting positive decimal numbers into twos complement, or do we assume that for any positive decimal number the twos complement is just itself:
e.g 7 = 0111 or 0000 0111 if in 8 bits?
This would also suggest then that the 4 step method above CANNOT be used for POSITIVE numbers?
(note that discussion here uses TCR for "Two's Complement Representation")
Unfortunately, the material you provided here (allmath.com) probably misunderstood what is "Two's Complement". There are many books have provide the canonical definitions for it, for the sake of convenience, it suffices to quote from Wikipedia:
The very first sentence:
Two's complement is a mathematical operation on binary numbers
, along with the definition for its mathematical operation:
w = -a_{n-1}2^{n-1} + a_{n-2}2^{n-2} + ... + a_{0}2^{0}
Take a binary number 11111001
as an example. Without TCR, we can interprate it as 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 1 = 249
. With TCR (i.e. applying the mathematical operation), we view it as -2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 1 = -7
.
Similiary, for another binary number, say 00000111
. Without TCR, we view it as 2^2 + 2^1 + 1 = 7
. And with TCR, we view it as 2^2 + 2^1 + 1 = 7
, which is identical to the former one.
Your 4-step operations are essentially: obtaining the TCR for the negation of a given number.
Again, taking 11111001
as an example, after your 4-step operations, it turns into 00000111
, which is 7
, i.e. the negation of -7 (11111001)
. Similiary, after your 4-step operations, 00000111
turns into 11111001
, which is -7
, i.e. the negation of 7 (00000111)
. Thus, the result of your 4-step operations is call Two's Complement Negation, instead of Two's Complement.
One reason might be: Nearly all computers adopt the TCR notion. Assuming a 8-bit computer, when we want to see how would our computer display the decimal number 7
, our computer would directly print out 7
, which is very intuitive for most people. However, for decimal number 249
, it usually does not "obey your commands", instead, it prints out the another value: -7
.
Conversely, if you want your computer to display -7
, accoding to Cornell University's Lecture Notes, the things you need to do are:
-7
, which is 7
;7
's binary representation (i.e. 00000111
) as input.Then our computer would get a binary representation 11111001
(it's 249
in decimal), and it would display it with -7
on your screen.
In this scenario, you could fairly say that, in an 8-bit computer using TCR, -7
is the alternative representation of the two's complement number 249
. Although this saying is not much canonical, it helps someone who just learned the notion of TCR for the first time.
In terms of the notion of TCR, please refer to Wikipedia, University Lecture Notes or CS books for more precise details, instead of many so-called "online crash courses".