Hexadecimal Addition/Subtraction and 2’s Complement
Updated: Jan 9
This article will cover unsigned addition and subtraction, and 2’s complement addition and subtraction in base 16 (hexadecimal). This article will also cover the topic of overflow in relation to these operations.
What is hexadecimal?
The hexadecimal system is a numeral system with a base of 16. It has 16 distinct symbols, 0-9 to represent their respective binary values, and A, B, C, D, E and F to represent 10-15 respectively.
For completeness, let’s begin with converting a hexadecimal number to its decimal equivalent. Each position in a hex number represents a power of 16, much like binary except we with base 16 instead. So let’s take 19FDE_16 for example. The decimal equivalent is calculated like so:
1) Write out what each symbol is going to be multiplied to base on its position
2) Now convert the letters to their decimal values
3) Now multiply and add and you have your answer!
Remember bases go up from right to left, not left to right!
Addition in Hexadecimal
You only carry in hex addition if your number exceeds 15. Other than that, it is almost the same as decimal addition. Let’s start with a small example.
Start from the right and add.
1) 6 + 3 = 9 with no carry
2) A + B = 10 + 11 = 21 (> 15), so there is a carry! Subtract 16 from 21 and carry a 1 over (this is like regular addition but instead of subtracting 10, just subtract 16 since we are in base 16).
3) 4 + 1 + 1 (carry) = 6
And you end up with your answer being
= 659_16 = 1190_10 + 435_10 = 1625_10
Taken from a previous ECS 50 course is this practice problem:
Now let’s do part a.
Now, overflow is when you do not have enough space to store your entire answer, so if the leftmost column number is over 15, there is an overflow for that calculation. For this specific part, there is no overflow.
Subtraction in Hexadecimal
This is like regular decimal subtraction but the borrowed number is different. In the decimal system, you borrow a group of 10_10, but in the hexadecimal system you borrow a group of 16_10.
Let’s subtract our earlier example:
Now let’s do part b.
Again, there is also no overflow here!
To get the 2’s complement negative notation of a number, write out the binary form of the number, invert the bits and add 1.
Suppose we want to know what -14 looks like, using 7 bits for simplicity.
1) First we write 14 in binary: 0 0 0 1 1 1 0
2) Then we invert the bits, 0 becomes 1 and 1 becomes 0: 1 1 1 0 0 0 1
3) Now we add 1:
This is -14 in 2’s complement notation. NOTE: to know if a number is negative in 2’s complement, the first bit is known as the ‘sign’ but, with 0 meaning the number is positive and 1 meaning the number is negative.
Now to convert from hex to 2’s complement, you want to convert from hex to binary, then binary to 2’s complement (if you want the negation of the number you are converting).
To convert from hex to binary, use this table:
Each hex digit converts into 4 binary digits: Lets take (4E)_16 for example
4_16 = 0100_2
E_16 = 1110_2
2’s complement is useful for subtraction, but 2’s complement addition is the same as regular addition, but the overflow is different.
Let’s do part c.
But there is overflow for this!
2’s Complement Overflow
Overflow occurs when two signed 2’s complement numbers are added and 1) if both operands are positive, but the result is negative or 2) when both operands are negative but the result is positive. So let’s look back at part c.
Now onto part d, 2’s complement subtraction.
First write out the binary forms of both numbers.
Now get the 2’s complement of B8FDE3.
Invert the digits: 010001110000001000011100
Add 1: 010001110000001000011101 (negation of B8FDE3).
Now “add” A689BE + (-B8FDE3).
So your final answer is ED8BD8_16. There is no overflow since they are different signed operands!