5. Two’s Complement Representation
: This is a form of representing Binary
numbers which makes it very easy to store both positive and negative numbers
and to find the binary representation of the negation of a given number.
a. The leftmost bit (most significant bit) is the “sign bit” which means that it
tells you whether the number is positive or negative. ‘1’ means that the
number is negative and ‘0’ means it is positive.
b. The rest of the digits represent the absolute value of the number in the
normal Base 2 way.
c. This means that given n-bits to represent a number, we can only use n-1
bits to represent the number. This leads to a largest positive number of
2
(n-1)
- 1 and smallest negative number as -2
(n-1)
. Can you prove this to
yourself? Try a few examples.
d. Now how do you negate a number?
i. Take the original two’s complement representation of the number
and flip all the bits (1 becomes 0 and vice versa)
ii. Add 1 to this new number and this leads to the negative of the
original number.
e. To convert a two’s complement number to decimal, you first check the
leftmost bit to see if it is positive or negative. If it is positive, you convert
to decimal as normal. If it is negative, negate the number, convert to
decimal as normal, then add the negative sign back in front of it.
f. (4)
10
= (0100)
2
in Two’s Complement form. Now let’s apply our little
algorithm! Flipping bits yields (1011)
2
and when you add 1, you get (1100)
2
.
Convert it to decimal and you will see that 1100 is the two’s complement
form of -4.
g. Arithmetic works pretty much the same as normal binary numbers.
Little Endian and Big Endian!
These are ways to store numbers or data in memory addresses. Let’s use a 16-bit word
as example , (0xFEED)
16
in this case. Let’s also assume we are storing this word starting
1
at address 0x4000. We store these words in terms of bytes and not bits, so we need
some conversion. Recall that 16 bits is 2 bytes (since 1 byte is 8 bits). The word is stored
in pairs to make up the required 1 byte per memory location so our two parts will be
‘FE’ and ‘ED’.
1
In this case, ‘word’ means any number in Base-16 or Hexadecimal. So, a 16-bit word is a hexadecimal
number with 16 bits or 4 values.