2's Complement base system - binary

I'm doing a question about 2's complement and I was just wandering about the place value of 2's complement. For example, in binary, the place value goes like 1 2 4 8 16 32 and doubles by two. but I'm not sure what the place value is for negative numbers.
The question I've got is asking you that is this number 10110001 was a 2's complement integer what would it be. I under than that you flip the numbers and add one but how can I actullay find the value of this?

In the 2's complement system, the most significant place ("sign bit", though this term causes confusion since it isn't purely a sign) has the weight -2k where k is its index, for example for 8 bits:
weights: -128 +64 +32 +16 +8 +4 +2 +1
example: 1 1 1 1 1 1 0 0 (0xFC) = -128+64+32+16+8+4 = -4

Related

How to add 1 for two's complement when you have a 1 in the first bit? [duplicate]

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.

Subtracting binary with numbers of different lengths

I am trying to solved: 1111 – 10010 (binary)
I would like to use two's compliment to solve it. I realize that the answer will be negative, but I don't know how to get it. I tried putting a zero before the first number (01111) to give the numbers equal number of 1s and 0s. Also, how will I know the answer is negative?
01101
+ 00001
____________
01110 <-- two's compliment
01110
+01111
________
11101 //this isn't right
I think the easiest way to solve this problem is to beak it into small steps.
My first assumption is that you are trying to solve 15 (1111 (binary)) - 18 (10010 (binary))
I find the easiest way to do subtraction in two's complement is by the method of complements which is instead of trying to subtract the positive 18 from the positive 15 ( +15 - (+18)), we instead add negative 18 to positive 15 ( +15 + (-18) ). This has the same result but is easier to do in two's compliment ( note: you can't do this if your number system does not have negative numbers)
So we have to take the number 15 and -18 and convert them into two's complement numbers. Since 18 is represented in binary with 5 bits we need to use at least 6 six bit to represent -18 in twos complement.
To convert -18 into two's complement we take 18 in two's complement 010010 flip those bits (turn the 0s into 1s and 1s into 0s) 101101, and then to add one to the flipped bits using binary addition
1 (carried digits)
101101 (-19 (flipped 18 ))
+ 000001 (1)
_________
101110 (-18)
To convert 15 into two's complement we take 15 in binary (1111) and then we add zeros on the left until it has the same amount of digits as -18 (101110) 001111
Now that we both numbers in two's complement we can add them together using binary addition
111 (carried digits)
001111 (15)
+ 101110 (-18)
_________
111101 (-3)
This give us -3 in two's complement which is the correct answer (15 - 18 = -3).
You can learn about two's complement looking at the twos complement wiki page

Converting hex to binary and one and two's complement in 16 bits

I am trying to convert FFAD (hex) to a decimal value and then do one and two's complement on it. FFAD is represented as a 16 bit integer.
When I convert FFAD to base 2 I get 1111111110101101.
My problem is how I know if it is a negative number or not?
I have the binary, now to do ones complement normally I would change the last bit from a 0 to a 1 and then flip all the bits, but as a 16 bit integer I don't have any more available bits. Since the 16th bit is a 1 does that mean it is a negative number? How would I do the complement of that? I am just all around confused with this problem and any pointers would be greatly appreciated.
Its negative because it is left padded with ones. Left padding with zeros would indicate a positive number. For example, here are the decimal values for three bit numbers:
011 = 3
010 = 2
001 = 1
000 = 0
111 = -1
110 = -2
101 = -3
100 = -4
Notice, numbers that are left padded with ones are negative and numbers that are left padded with zeros are positive.
Standard procedure for 2's compliment is to negate your bits, and then add 1.
1111111110101101 becomes 0000000001010010. Then you add 1 to get 0000000001010011.

Adding and subtracting two's complement

Using six-bit one's and two's complement representation I am trying to solve the following problem:
12 - 7
Now, i take 12 in binary and 7 in binary first.
12 = 001100 - 6 bit
7 = 000111 - 6 bit
Then, would I then flip the bit for two's complement and add one?
12 = 110011 ones complement
+ 1
-------
001101
7 = 111000 ones complement
+ 1
---------
111001
then, add those two complement together
001101
+111001
-------
1000110 = overflow? discard the last digit? If so I get 5
Now, if I have a number like
-15 + 2
I would then add a sign magnitude on the MSB if it's a zero?
like:
-15 = 001111 6 bit
Would I add a 1 at the end here before I flip the bits?
= 101111
Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:
12 001100
-7 111001 -- to get this, invert all bits of 7 (000111) and add 1
----------
5 1000101
Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.
For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:
15 001111
110000 -- inverted bits
110001 -- add 1
Now do the addition as usual:
-15 110001
2 000010
-----------
res 110011
To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):
res 110011
001100 -- inverted bits
001101 -- add 1
Hence the magnitude is 13 as expected.
No. The algorithm for two's complement doesn't change based on where the negative value is.

Decimal to binary number conversion

What is the binary form of -10? How it is calculated?
To convert -10 (decimal) to binary:
Repeatedly divide the absolute value (|-10| = 10) of the number by 2 until you get 0 in the quotient:
(10 / 2 = 5 R 0)
(5 / 2 = 2 R 1)
(2 / 2 = 1 R 0)
(1 / 2 = 0 R 1) // zero is the value in the quotient so we stop dividing
Place the remainders in order to obtain the binary equivalent:
1010
For an 8-bit cell the answer is 0000 1010, 16-bit cell 0000 0000 0000 1010, and so on.
Take the one's complement by inverting the bits (we will assume an 8-bit cell holds the final value):
0000 1010
1111 0101 // bits are inverted
Now take the 2's complement by adding 1:
1111 0101
+ 1
----------
1111 0110 // final answer
What happens with a 4-bit cell?
The one's complement would be:
1010
0101 // inverted bits
Taking the 2's complement produces:
0101
+ 1
----
0110 // final answer for a 4-bit cell
Since the number should be negative and the result does not indicate that (the number begins with 0 when it should begin with a 1) an overflow condition would occur.
Take the binary form of 10, invert all the bits, and add one.
10 0000 1010
invert 1111 0101
add 1 1111 0110
Follow this link. You can find the binary form of a negative number say -n by finding out the two's complement of n.
Your question has no "right" answer. First, you have to define the representation you want to use. Do you want to use two's complement, ones' complement, sign-magnitude, or something else? Then you have to define how many bits to use.
Let's say we are working with 5-bits wide representations.
+10: 0 1 0 1 0
Let's look at sign-magnitude system. In this system, the most significant bit (bit 5) is 1 if a number is negative, and 0 otherwise. The rest of the bits represent the magnitude (absolute value) of the number.
So we get:
-10: 1 1 0 1 0 (sign-magnitude, 5 bits)
Let's look at ones' complement now. Here, a negative number is represented by just changing 1s to 0s and vice-versa (hence the name ones' complement—you complement a number with respect to a long sequence of 1s).
So we get:
-10: 1 0 1 0 1 (ones' complement, 5 bits)
Finally, let's look at two's complement system. In this, we take a number in its ones' complement system, and then add 1 to it.
So we get:
-10: 1 0 1 0 1
1
---------
1 0 1 1 0 (two's complement, 5 bits)
---------
Thus, the binary representation of a negative number depends upon the system we use, and the number of bits available to us.
Also, you might have noticed the position of the apostrophe in ones' complement and two's complement. Why is is not one's complement or twos' complement? Then answer, from Knuth:
A two's complement number is complemented with respect to a single power of 2, while a ones' complement number is complemented with respect to a long sequence of 1s. Indeed, there is also a "twos' complement notation," which has radix 3 and complementation with respect to (2...22)3