Thursday, 15 January 2015

c - Overflow in bitwise subtraction using two's complement -



c - Overflow in bitwise subtraction using two's complement -

when performing bitwise subtraction using two's complement, how 1 know when overflow should ignored? several websites read stated overflow ignored, not work -- overflow necessary problems -35 - 37, digit needed express reply of -72.

edit: here's example, using above equation.

35 binary -> 100011, find two's complement create negative: 011101

37 binary -> 100101, find two's complement create negative: 011011

perform add-on of above terms (binary equivalent of -35 - 37):

011101 011011 ------ 111000

take two's complement convert positive: 001000

the above many websites (including academic ones) reply should be, ignore overflow. incorrect, however.

an overflow happens when result cannot represented in target info type. value -72 can represented in char, signed 8-bit quantity... there no overflow in example. perhaps thinking borrow while doing bitwise subtraction... when subtract '1' '0' need borrow next higher order bit position. cannot ignore borrows when doing subtraction.

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

going right left to the lowest degree important significant bit can subtract each bit in +37 each bit in -35 until bit 5 (counting starts @ bit 0 on right). @ bit position 5 need subtract '1' '0' need borrow bit position 6 (the next higher order bit) in -35, happens '1' prior borrow. result looks this

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

the result negative, , result in 8-bit two's complement has high order bit set (bit 7)... negative, there no overflow.

update:i think see confusion is, , claim reply here adding , subtracting two's complement wrong when says can discard carry (indicates overflow). in reply subtraction converting sec operand negative using two's complement , adding. that's fine - carry doesn't represent overflow in case. if add together 2 positive numbers in n bits (numbered 0 n-1) , consider unsigned arithmetic range 0 (2^n)-1 , carry out of bit position n-1 have overflow - sum of 2 positive numbers (interpreted unsigned maximize range of representable positive numbers) should not generate carry out of highest order bit (bit n-1). when adding 2 positive numbers identify overflow saying

there must no carry out of bit n-1 when interpret them unsigned and the result in bit n-1 must 0 when interpreted signed (two's complement)

note, however, processors don't distinguish between signed , unsigned addition/subtraction... set overflow flag indicate if interpreting info signed then result not represented (is wrong).

here a detailed explanation of carry , overflow flag. takeaway article this

in unsigned arithmetic, watch carry flag observe errors.

in unsigned arithmetic, overflow flag tells nil interesting.

in signed arithmetic, watch overflow flag observe errors.

in signed arithmetic, carry flag tells nil interesting.

this consistent definition of arithmetic overflow in wikipedia says

most computers distinguish between 2 kinds of overflow conditions. carry occurs when result of add-on or subtraction, considering operands , result unsigned numbers, not fit in result. therefore, useful check carry flag after adding or subtracting numbers interpreted unsigned values. overflow proper occurs when result not have sign 1 predict signs of operands (e.g. negative result when adding 2 positive numbers). therefore, useful check overflow flag after adding or subtracting numbers represented in two's complement form (i.e. considered signed numbers).

c bit-manipulation bitwise-operators

No comments:

Post a Comment