0

I have a signed binary number "a", and I want to use a subtractor to determine if this number is greater than or equal to, or less than another number "b". this is a related topic

In the thread above, the answer uses the carry out bit.

In my opinion, we can just use the MSB, since in 2's complement computation, the MSB tells the sign out the output. If the output sign is 1, we know we must have a less than, while the sign is 0, then we know we have a greater or equal to. But then I have the problem of overflowing.

For example, if I have two 5 bit number

5: 00101
-5: 11011 
dec = 6+(-5) = 1
  00110
 +11011
  =====
 100001
carry = 1, MSB = 0, so MSB =0 or carry = 1 is greater than or equal to.

dec = 5-6=-1
  00101
 +11010
  =====
 011111
carry = 0, MSB = 1, so MSB =1 or carry = 0 is less than.

dec 6 - (-10) = 16
 00110
+01010
 =====
010000 

carry = 0, MSB =1, this would be wrong if i evaluate to 6 less than -10.

So i have four questions:

1. should I use carry out or MSB as the condition?
2. what to do for the overflow condition?
3. how could i design a general comparator using the subtractor method?
4. I know I could make a explicit if else statement on the sign bit of the input and output and determine if one is bigger or less than the other. But is there a way of not doing those if and else, but just use the MSB or carry flag for the judgement?

Thanks a lot for any help!

GGinside
  • 63
  • 6
  • http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt shows how to compute carry and signed-overflow flags for addition. It also mentions subtraction somewhat. – Peter Cordes Jun 09 '20 at 05:35
  • thansk, the link does tell me how to detect overflow, which i knew. I want to know how to utilize this information for a general comparator – GGinside Jun 10 '20 at 00:46

0 Answers0