What is a 32-bit two's complement? - binary

I'm really confused about the term "32-bit twos complement"
If I have the number 9, what is the 32-bit twos complement?
9 = 1001
Two's complement = 0111
32-bit 9 = 0000 0000 0000 0000 0000 0000 0000 1001
Two's complement = 1111 1111 1111 1111 1111 1111 1111 0111
That result is so ridiculous! It's way too large! Is that really how you're supposed to do it?

The most common format used to represent signed integers in modern computers is two's complement. Two's complement representation allows the use of binary arithmetic operations on signed integers.
Positive 2's complement numbers are represented as the simple binary.
Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero.
Your 2's complemented output is equivalent to -9 (negative 9).
Edited:
You are asked to perform 32-bit operation hence it should be 1111 1111 1111 1111 1111 1111 1111 0111
For signed number, leftmost bit represents the sign of the number. If leftmost bit (LSB) is 1 then the number is negative otherwise it's positive. So, your 32-bit 2's complemented number is negative and it's -9. Simply, performing 2's complement on a number is equivalent to negating it
i.e. it makes a positive number into negative and vice versa.
For more browse the link:
http://www.tfinley.net/notes/cps104/twoscomp.html

Related

How is this sum equal in unsigned binary and twos complement binary?

I'm reading through a computer architecture book and came across the following:
Assume you have a 8-bit cell. So there are 256 possible integer values. Non-negatives run from 0 to 127. Assuming two's complement binary representation, what is the sum of 97 + 45?
Unsigned is clearly 142, you can do it as 97 + 45 in decimal, or:
0110 0001
0010 1101 ADD
--------------
1000 1110
But when you perform two's complement, you take that result (1000 1110) and determine that it is negative since the sign bit is 1. Then take the one's complement of it:
NOT 1000 1110 = 0111 0001
Then determine its two's complement:
0111 0001
0000 0001 ADD
--------------
0111 0010
This number is 114 but because our original number had a 1 in the sign bit, it's -114.
Question(s):
Why go through all the trouble of having the two's complement to find -114? Since 97 and 45, why not just find the sum of the two positive integers as an unsigned value which fits within the range of an 8-bit cell (1111 1111 being 255). Is it just because the question asks for the two's complement?
Is -114 equivalent to 142? I believe so if you take the two's complement number line, yo get 142-256 which is -114. From this, I dont understand why you would want to even use two's complement if you are summing two positive values!
A 1's complement just means flip all the bits, a 2's complement means negate the value. So 1's complement for 8-bit results effectively in 255 - x and 2's in 256 - x. You achieve the result of a 2's complement by doing a 1's complement and adding 1.
The 142 in 8-bit is equal to -114. Don't get too confused about it.

Binary into 2's compliment

if I convert the binary number 000000 into 2's compliment I will get
1's compliment (invert) = 111111
2's compliment (add +1) = here I run into a problem, does this return 000000 and the 1 gets discarded or does this return 1000000?
Thanks in advance!
It discards the 1. The condition is similar to the Arithmetic Overflow.
Strength of two's complement is that it helps us retain a binary representation when signed numbers are to be considered, because in mathematics, the value of 0 is same as that of -0. If we have to give up one entire bit just for sign, then, in a 4-bit word, 0000 would denote 0 and 1000 would denote -0, wasting one representation. The two's complement helps get rid of this. If we assume 4-bit words:
val -val bits of val two's complement bits of -val (1's complement + 1)
0 0 0000 0000 (1111+0001)
1 -1 0001 1111 (1110+0001)
2 -2 0010 1110 (1101+0001)
3 -3 0011 1101 (1100+0001)
...
7 -7 0111 1001 (1000+0001)
8 -8 (no rep) 1000 (0111+0001)
(note that for -8 you have one's complement of 8 in an unsigned manner, i.e.8 = 1000 and hence its one's complement is 0111).
Thus you gain a representation for -8 by making 0 and -0 have the same bit pattern, i.e. 0000. Using this, for n bits, we can represent all integer values between -2^(n-1) to 2^(n-1)-1.

converting binary, decimal, hexa etc. what is the role of bit?

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).

How can I check a negative solution of a twos complement subtraction equation?

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.

converting decimal to signed binary

Let's say I want to convert "-128" into binary.
From what I understand, I get the binary representation of "128", invert the bits and then add 1.
So 128 = 10000000
So the "inverse" is 01111111
So and "01111111" + "1" = "10000000" which is "-0" isn't it?
My textbook makes this seem so easy but I can't figure out what I'm doing wrong. Thanks for the help.
No, that's definitely -128 (in two's complement anyway, which is what you're talking about given your description of negating numbers). It's only -0 for the sign/magnitude representation of negative numbers.
See this answer for details on the two representations plus the third one that C allows, one's complement, but I'll copy a snippet from there to keep this answer as self-contained as possible.
To get the negative representation for a positive number, you:
invert all bits then add one for two's complement.
invert all bits for one's complement.
invert just the sign bit for sign/magnitude.
You can see this in the table below:
number | twos complement | ones complement | sign/magnitude
=======|=====================|=====================|====================
5 | 0000 0000 0000 0101 | 0000 0000 0000 0101 | 0000 0000 0000 0101
-5 | 1111 1111 1111 1011 | 1111 1111 1111 1010 | 1000 0000 0000 0101
You should be aware that there is no 128 in 8-bit two's complement numbers, the largest value is 127.
Where the numbers pass the midpoint is where the "clever" stuff happens:
00000000 -> 0
00000001 -> 1
: :
01111110 -> 126
01111111 -> 127
10000000 -> -128
10000001 -> -127
: :
11111110 -> -2
11111111 -> -1
because adding the bit pattern of (for example) 100 and -1 with an 8-bit wrap-around will auto-magically give you 99:
100+ 0 0110 0100
1- 0 1111 1111
===========
1 0110 0011 99+ (without that leading 1)
It depends on what your binary representation is -- ones complement, twos complement, sign-magnitude, or something else. The "invert bits and add 1" is correct for twos complement, which is what most computers these days use internally for signed numbers. In your example, "10000000" is the 8-bit twos-complement representation of -128, which is what you want. There is no such thing as -0 in twos complement.
For sign-magnitude, you negate by flipping the sign bit. For ones complement, you negate by inverting all bits.