Smallest Two's Complement in Binary - binary

I'm not sure if I can ask a binary question here but here goes..
We had this question on our midterm but our professor hasn't provided a correct answer for it. It's been driving me crazy and the final is coming soon so it might be a good idea to fill this gap. Thanks!
Find the smallest two's complement number that, when added to 0101 0101 would result in an overflow. Express your answer in binary.
My reasoning:
I found the range of the original binary 0101 0101 by converting it to an actual number and then added one to it. Then I converted the number that was 1 more than the range into 8-bit binary as my answer. However, this only earned me 3/6 marks. I have no idea what else I could've done. Any insights would be greatly appreciated!

The original binary is a positive number (0 sign bit). Overflow occurs when you add a positive number to it that changes the sign bit. It should be easy to see what the smallest number is using binary notation:
No overflow:
0101 0101
+ 0010 1010
---------
0111 1111
Overflow:
0101 0101
+ 0010 1011
---------
1000 0000
I have no idea if this is what your prof was looking for. (You can probably just subtract from 1000 0000 instead of looking at it as a pattern.)
EDIT Since you asked for an example (meaning something different from the above), here's how subtraction would work:
1000 0000 (the target overflow quantity)
- 0101 0101 (the original binary)
---------
0010 1011 (the smallest number that will overflow when added to original)

That number is 85 in decimal, so 128-85 is 43

Related

Why do some hex numbers have a 2s compliment but some do not?

I want to write out the 2s compliment of the hex 0x1234
As I understand the binary would look like: 0001 0010 0011 0100 and decimal 4660
I was under the impression changing the leading 0 indicates positive vs negative, however online calculators aren't giving me any answer so my understanding of this is wrong
Alternatively looking at 0x8001 as an example: the decimal would be 32769 and the binary 1000 0000 0000 0001
Online calculators return an answer of -32767 as the 2s compliment
Could anyone please offer an explanation or some other post that explains in detail this concept of 2s compliment? Thank you very much!
You get a number's two's complement by inverting all bits and then adding one to it. It must be agreed up front how many bits/digits are involved, i.e. what the word size is. For instance, the result will not be the same for 0x1234 and 0x00001234.
For your example (4 hex digits): 0x1234
In binary: 0b0001 0010 0011 0100
Invert bits: 0b1110 1101 1100 1011
Add one: 0b1110 1101 1100 1100
In hex: 0xEDCC

A 2's complement conversion

Honestly I'm doing some homework and got stuck with the 2's complement concept. So my question asked me to find the 2's complement for 492 and -1094(as 16-bit number) in both binary and hexadecimal. But I really got confused with the way it works. So I did 492's binary and mine was like 0000 0100 1001 0010 but I really got lost and don't feel right about my answer.. Can someone help me here... Thanks!
492 in binary goes to 0001 1110 1100
1*2^8 + 1*2^7 + 1*2^6 + 1*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
-1094 in binary goes to 0100 0100 0110
The trick to represent a negative number: you have to convert to binary and flip all the digits (0 becomes 1, and 1 becomes 0) and add 1.
So to calculate 2's complement, you just do the following:
For some number B, invert it's binary representation, and add 1.
For 492:
111101100
000010011 <-- invert
000010100 <-- add 1
Just do the same for your binary representation of -1094.

Mips subtraction negative overflow

Hexadecimal in mips: If I have 0x80000000 and I subtract it by 0xD00000000
my answer is 0X -50000000
is this possible in mips having a negative in or is there another way to write this? That is correct?
Remember, if you look at the most significant bit, you'll know if you are working with a negative number. For simplicity, let's just look at the four most significant bits.
0x80 = 1000 0000
0xD0 = 1101 0000
So those values are already negative. You want to subtract them, that is, 0x80 - 0xD0. Well, subtraction is addition (a-b = a + -b), and the whole point of 2's compliment is that you can add signed numbers and get the result you expect. So let's negate 0xD0:
1101 0000 # original value
0010 1111 # flip the bits
0011 0000 # add 1
If that didn't make sense, ask the all-knowing Wikipedia.
Now, we can add the values:
1000 0000
0011 0000
---------
1011 0000
So if I've done it right, 0x80 - 0xD0 = 0xB0. It's still a negative number (but you don't put a minus sign in front of it, it's implied by the MSBit). And that makes sense, because 0x80 is a very negative number, and 0xD0 is a less negative number than 0x80. (Really, it is... 0xFF is -1, the least negative number.)

What is lower and higher bits?

Can anyone tell me what is lower and higher bits?. How to identify a higher and lower bit?. Below is binary form. How does 0110 is higher bit in it?.
0110 0111 1100 1010 1100 0111 1001 1011
Just like in decimal, higher places are generally written to the left in binary. So if you see 0111, the 0 is the high bit. So this would represent 7 in decimal. The same applies when spaces are used, just like when commas (or dots, depending on your locale) are used to separate groups of digits in decimal.

Binary addition/subtraction

I am having a bit of trouble understanding Carry Flag (CF) and Overflow Flag (OF).
Here are some sample problems I am working on:
1. 1011 1111 2. 1111 0111 3. 0111 1110 --> 0111 1110
+ 1011 0111 + 1101 1101 - 1011 0000 --> + 0100 1111
___________ ___________ ___________ + 1
0111 0110 1101 0100 ___________
1100 1110
The carryout of the sign position is 1 and the carry in to the sign position is 0, so OF = 1?
The carryout of the sign position is 1 and the carry in to the sign position is 1, so OF = 0?
The carryout of the sign position is 0 and the carry in to the sign position is 1, so OF = 1?
I guess I am having trouble understanding an unsigned overflow and the appropriate CF value.
Disclaimer: I'm not an expert (or even a user of this level of code :) ).
I believe the carry-flag makes sense for unsigned data, and the overflow-flag makes sense for signed data.
Both will always be generated, but it is up to you to determine if you consider the values unsigned, or two's complement, so it is up to you which flag you pay attention to.
From: http://en.wikipedia.org/wiki/Overflow_flag
Internally, the overflow flag is usually generated by an exclusive or of the internal carry into and out of the sign bit. As the sign bit is the same as the most significant bit of a number considered unsigned, the overflow flag is "meaningless" and normally ignored when such numbers are added or subtracted.
The sign bit is the most significant bit (the one farthest left).
Exclusive or (XOR) is:
If neither: 0
If either: 1
If both: 0
Carry-in to the sign bit is when the 2nd most significant bits, when added, produce a value to be carried over to the next column.
Carry-out is whether carry must be done when adding the most significant bits (the sign bits, if the numbers are two's complement) together.
XOR those two values, and you should end up with the value for your overflow flag after a given addition.