Bit overflow overflow using two additions

When performing bitwise subtraction using two additions, how do I know when to ignore overflow? Several websites that I read indicate that overflow is simply ignored, but this does not always work - overflow is necessary for problems such as -35 - 37since an extra digit is needed to express the answer -72.

EDIT: Here's an example using the above equation.

35to binary → 100011, find two additions to make them negative:011101

37to binary → 100101, find two additions to make them negative:011011

Complete the above terms (binary equivalent -35 - 37):

011101
011011
------
111000

Let's take two additions to convert back to positive: 001000

Above, many websites (including academic ones) say that the answer should be, since you ignore overflow. This is clearly wrong.

+5
source share
1 answer

Overflow occurs when the result cannot be represented in the target data type. The value -72 can be represented in char, which is a signed 8-bit amount ... in your example there is no overflow. Perhaps you are thinking about borrowwhen doing bitwise subtraction ... when you subtract '1'from '0', you need borrowhigher order bits from the next position. You cannot ignore entries when performing subtraction.

-35 decimal is   11011101 in two complement 8-bit
+37 decimal is   00100101 in two complement 8-bit

, +37 -35, 5 ( 0 ). 5 '1' '0', 6 ( ) -35, '1' .

-35 decimal is   11011101 in two complement 8-bit
+37 decimal is   00100101 in two complement 8-bit
                 --------
-72 decimal is   10111000 in two complement 8-bit

, 8- ( 7)... , .

. , , , , , , discard the carry (indicates overflow). , , , . , . N ( 0 N-1), range 0 to (2^N)-1, N-1, - ( unsigned ) ( N-1). ,

  • N-1, unsigned
  • N-1 ( )

, , unsigned /... , , , ().

. -

  • unsigned .
  • unsigned .

  • signed .

  • signed .

,

. , , , . , . , , (, ). , (.. ).

+5

All Articles