I've built a four bit adder/subtractor utilizing 4, 1 bit full adders and the input and output are twos complement numbers.
If X=0111 and Y=1000 their sum is obviously 1111.
In decimal this is equivalent to 7 + 8 thus 15 which is what the sum results in.
I am confused however if this result needs to be translated back into "regular" binary by flipping the bits and adding one? So that the answer would be 0001 representing 1 in decimal instead. And that Y in decimal before translation was actually 0110 representing 6 thereby yielding the following in binary 7-6 = 1. If anyone could point me in the right direction I would appreciate it!
It appears you've got the conversion for Y wrong. Y = 10002 = -810.
To represent -6 you take 0110, flip the bits to get 1001 and add one, so Y = 1010. (And 0111 + 1010 = 0001 as you expect.)
To go back, flip the bits of 1010 = 0101 and add one giving 0110 = 6.
Edit to answer your follow-up question:
Let:
X = 0111
Y = 1100
X + Y = 0011 (ignoring overflow)
So whatever we're adding, it equals 3. We know that X = 7.
Y = 1100 => 0011 + 1 = (negative)0100 = -4
7 + (-4) = 3
No translation is necessary, just represent the positive and negative numbers correctly. I think your confusion is coming from the fact that we're "negating" the negative numbers to find the absolute value of that number and sticking a negative sign in front of it, as in the conversion of Y above. That's because negative numbers in 2's complement aren't as readable as positive numbers, but 0100 is still +4 and 1100 is still -4.
Related
I understand the concept of using two's complement to represent a negative value which can then be used in addition to find the resulting value.
ex.
x = 15 and y = 10, in binary x = 1111 and y = 1010
for calculating x - y, we represent y as a negative using twos complement which results y = 0101 + 0001 = 0110 then carrying out the addition x + (-y) = 1111 + 0110 = 0101 which translates to the expected result 5.
All is well and fine until I try to use the same method to calculate y - x which ends up resulting in 1011 translating to 11, not the expected result -5. Is there a way to properly calculate the addition of two binary numbers resulting in a negative value?
Think of it this way; y - x is equivalent to -x + y. For that reason, we can first negate x, which is 15 (1111 in binary), which becomes 0001 after negation. We can then add that value to y (which is 10, 1010 in binary), which results in 1011. Because this value is negative, we then flip the bits and add 1 to make it positive, and we get 0101, which is 5 in base-10. This means that our result was -5, which is correct.
I think you were correct everywhere except where you stated that it translates to 11.
If I have got 1101 0010 0010 0010 on a 16bit system and want to convert it to decimal the result is -11,742 (teacher told me)
But if I enter the binary number into my calculator I get 53794.
Why is this the case? How does the system (8bit, 16bit, 32bit, 64bit) affect this?
I tried to convert the binary number with hand (2+32+512+4096+16384+32768 = 53794) but only to confirm my calculator....
It's because computer arhitmetic is based on two's complement
You shoud read about method of colpement
For example in complement decimal number natural subtraction looks like that (using 4 digits):
0000
0001 -
9999 =
It's obviously -1 because in complemental system first digit (in big endian of course, you shoud be familiar with endianness) defines sign. For example in decimal complement 4 is 4, 4 is 4, 3 is 3, 2 is 2, 1 is 1, 0 is 0, 9 is -1, 8 is -2, 7 is -3, 6 is -4 and 5 is -5.
It's because in complement number system you always operate on same number of digits but you are adding and substracting as always except ignoring overflowing result. For example with two decimal digits:
04
05 -
...9999 =
As I told result are only two digits (we added two digits) so the result is 99. As I also mentioned before first number is interpreted as -1 (not 9). Because it is positional number system we can easy convert this result to our normal decimal system just summing digits multypling by correct power's of ten:
10^1 * 9 + 10^0 * 9
//as I told - first nine is -1 so:
10^1 * -1 + 10^0*9 = -10 + 9 = -1
Now try with first result:
9999 = 9 * 10^3 + 10^2 * 9 + 10^1 * 9 + 10^0 * 9
//first nine is -1 so:
-1 * 10^3 + 999 = -1000 + 999 = -1
Now in binary it is much simpler because first number just tells as what sign is. Try with 8 bit number:
1111 1111
It's obviously -1 because if we add 1 the result will be 0. You may say I'm laying becaus you can simply make following addition:
1111 1111
0000 0001 +
1 0000 0000 =
But it's not correct in complement system. It's because in complement system first digit not only describe sign of number but also all number can be infinitly expanded by that digit! It's why id complement decimal 99 = 9999 ! It also means that we can easy expand 8 bytes number to for example 32 bytes!
For example try with numbers 17 and -1. You are now familiar with -1. In 8 bytes two's complemex -1 is:
1111 1111
Expanded to 32 bytes it gives us:
1111 1111 1111 1111 1111 1111 1111 1111
To convert it to normal decimal system we need to do 2^31 + 2^30 + ... + 2^0 but (as I mentioned) first 1 is -1 so it's exactly -1 * 2^31 + 2^30 + .. + 2^0 if you compute this it's exactly -2147483648 + 2147483647 = -1 !
0001 0001 (decimal 17)
Expanded to 32 bites gives:
0000 0000 0000 0000 0000 0000 0001 0001
And we can agree it's still 17 :)
To provide I'm not some crazy, just IT student I'm going to present C++ code that does exactly that stuff:
#include <iostream>
using namespace std;
int main(void)
{
char eightBits = 255; //eightBits := 0x11111111
int eightBytes = eightBits; //eightBytes becaming 255, right?
cout <<eightBytes <<endl; // MAGIC !
return 0;
}
Basically, it depends on how much space there is. The most significant bit is used to determine whether or not the number is negative.
In a 32-bit system, the two following numbers are equivalent:
1101 0010 0010 0010
0000 0000 0000 0000 1101 0010 0010 0010
As a signed 16-bit value, the 1 at the beginning denotes that it's a negative number. To get its value, flip each bit, then add one.
1101 0010 0010 0010
0010 1101 1101 1101
0010 1101 1101 1110
= 11742
As an unsigned 16-bit value, all sixteen bits are used to determine the magnitude. This would result in 53794.
Reference
The unpoken bit here is about the sign. It is stored on the front end and denotes the sign.
http://www.binaryconvert.com/result_signed_short.html?decimal=045049049055052050
More succinctly, a 16bit 'word' or integer is usually called a 'short' on most systems and represents signed number between −32,768 and +32,767. This is the 'binary' number to which your teacher is referring, which is not stored directly as a binary number, but in binary format. There are actually only 15 spots to store the value and one that must be reserved for the sign. Otherwise, an unsigned value can use all 16 spots for numbers and thus can go to from 0 to +65,535.
There are different ways to represent binary numbers:
Unsigned (what you are doing)
Signed
1's complement
2's complement (what you are supposed to do according to your teacher)
Digital systems generally use 2's complement representation.
To get decimal number for a binary number in 2's complement:
1. First bit is the sign bit. If the number starts with 0, it is positive, otherwise it is negative.
2. If the number is positive, its decimal value is unsigned value of the remaining bits other than the sign bit. If the number is negative, its absolute decimal value = (signed value of the complement of the remaining bits)+1.
In computers, it is common to represent "two's complement" signed numbers by assuming that the leftmost bit is duplicated an infinite number of times, so that the bit sequence (1101 0010 0010 0010) is regarded as (1111....1111 1101 0010 0010 0010). Although an infinite string of ones may seem like an infinitely large number, adding one to such a string will yield an infinite string of zeroes (i.e. 0). Thus, an infinite string of ones is the additive inverse of 1, and thus has an effective value of -1. An infinite string of ones followed by some number of zeroes, will be the additive inverse of a single 1 followed by that number of zeroes (so in your example, the leftmost "1" has an effective value of -32768 rather than +32768). While the fact that applying the power-series formula to 1+2+4+8+16+32+64... yields -1 is often regarded as an anomaly, it is consistent with the way that "two's complement" math works.
Note that while many descriptions of two's complement math simply describe the sign of the uppermost bit's "scaling factor" as being inverted (e.g. -32768 rather than +32768), thinking in terms of left-padding with copies of the leftmost bit makes it easier to understand why signed numbers behave as though the leftmost number is duplicated (e.g. converting a negative 16-bit integer to a 32-bit integer fills the leftmost 16 bits with ones).
Hey guys I'm still trying to get the hang of twos complement arithmetic and I can get the correct answer, since I'm working on practice problems with solutions.
When I take the answer that's in binary, I can't seem to equate it out to the decimal answer before applying twos complement and adding the numbers.
000100-111001 In decimal it's 4 - 57= -53
Becomes
0001000+000111 which would be 4 + (-57)?
Giving a solution of 001011
How can 001011 be proven as equaling -53?
Thanks!
To answer your question, first 001011 is not equal to -53. This is the wrong answer. We know it must be positive since the highest order bit is a 0, not a 1. 001011 is actually equal to 11 (in base ten).
Let's do 4 - 57 now as an example. This is the same as 4 + (-57). Converting to binary (I will use just a byte for this example) we get: 4 is 0000 0100, 57 is 0011 1001. To convert 57 to negative 57 use two's complement:
1. Negate it: 1100 0110
2. Add one: 1100 0111So now we get the following equation:
0000 0100
+ 1100 0111
------------
1100 1011
We achieve the answer by simply adding down the rows. The answer we have gotten is 1100 1011. We know it is negative because the highest order bit (which here is leftmost) is a 1. To find its magnitude, we apply two's complement:
1. Negate: 0011 0100
2. Add one: 0011 0101
And this is equal to 53 in base ten.
Another way to see if it is correct is to add it with the positive version of the number.
1100 1011
+ 0011 0101
------------
10000 0000
Since two's complement is defined as the number subtracted from 2^n where n is the number of bits, you will always get this result for the sum. Knocking off the 1s digit it's interesting how what remains is just 0 - and any number plus its negative is zero.
I have answered the question but i could not understand the part where its says get the resulting sum in 16-bit binary, check your answer by converting the sum into decimal.
1.Add ‘A’ and ‘B’ in binary to get the resulting sum in 16-bit binary, check your answer by converting the sum into decimal.
Decimal number = 58927634
A= 5892 to Binary 1011 1000 0010 0
B= 7634 to Binary 1110 1110 1001 0
a + b = 11010011010110
thanks alot
Regards
Let me explain:
A = 00005892
B = 00007634
in base 10 number system, with 8 decimals to represent it.
and...
A = 0001 0111 0000 0100
B = 0001 1101 1101 0010
in base 2 number system, with 16 bits to represent it.
I hope it makes sense to you now.
And the sum of A and B, in base 2 number system with 16 bits to represent it....
0011 0100 1101 0110
although, we needed just 14 bits....
Your answer is correct, but its width is 14 bits long.
Or, what is the range of numbers that can be represented on a 4-bit machine using 2s-complement?
That would be -8 to +7
The range is -8 to 7, or 1000 to 0111. You can see the full range here.
4 bits (using 2's complement) will give you a range from -8 to 7.
This should be straightforward to work out yourself.
Range in twos complement will be:
-1 * 2 ^ (bits - 1)
to
2 ^ (bits - 1) - 1
So for 4 bits:
-1 * 2 ^ (4 - 1) = -1 * 2 ^ 3 = -8
to
2 ^ (4 - 1) - 1 = 2 ^ 3 - 1 = 7
Also, if you are interested and for others maybe browsing this question -
twos complement is used for easy binary arithmetic:
to add - you just add the two numbers without conversion and disregard the overflow:
-6 + 7 = 1
is
1010 = -6
0111 = 7
------
(1)0001 = 1 (ignoring the overflow)
...and more yet - to convert a negative binary number to its opposite positive number:
if the sign bit (highest order bit) is 1, indicating negative... reading from least significant to most significant bit (right to left), preserve every bit up through the first "1", then invert every bit after that.
So, with 8 bit
10011000 .. becomes
01101000 (* -1) = 104 * -1 = -104
and that is why 10000000 is your lowest negative number (or in X bit 1000.all zeroes..000), it translates to unsigned 10000000 * -1 = -128
Maybe a long winded answer but to those without the 1s and 0s background I figure it is useful
Well let's dissect this question.
Firstly, the question should be framed as - "The least*(because it is negative, so biggest is not the right usage)* possible negative number to be stored in 4-bit would be?"
Numbers are of two types -
Signed (holds both + and - numbers )
Unsigned (holds only + numbers)
We will be using binary representation to understand this.
For Unsigned -
4-bit minimum number = 0000 (0)
4-bit maximum number = 1111 (255)
So range would be Range : 0-15
For Signed 4-bits, first bit represent sign(+/-) and rest 3-bits represent number.
4-bit minimum will be a negative number.
So replace the first bit(MSB) with 1 in 0000(least unsigned number), making it 1000.
Calculate decimal equivalent of 1000 = 1*2^3 = 8
So, number would be -8 (- as we have 1 as the first bit in 1000)
4-bit maximum will be a positive number.
So replace the first bit(MSB) with 0 in 1111(largest unsigned number), making it 0111.
Calculate decimal equivalent of 0111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
So, number would be +7 (+ as we have 0 as the first bit in 0111)
Range would be -8 to +7.