math - Binary arithmetic - addition with overflow -


when need subtract 2 numbers (x-y), can take 2's complement of y , add x. let's our system represents integers using byte (8 bits).

x = 7 = 00000111 y = 5 = 00000101 

2's complement of 5

11111010 + 1 = 11111011 

adding 2 =

 00000111  11111011 __________ 100000010  

there carryover. how 1 deal carryover?

if using 8 bits, means have range of -128 127. 7 , -5 , sum not fall outside range. not overflow.

that depends on trying do

  1. if computing simple/single +/- operations

    then overflow ignored

  2. when need handle overflow/underflow

    for example if need clamp result reason (usually safety of result range ...) carry flag of alu marks if overflow underflow occur. after set result max positive or negative value depending on inputs sign,magnitude , operation (+,-). aome platforms have instructions automatically (saturated add,sub).

    another reason making bigint operations in case carry added +/-1 higher operation (sign depends on operation)... result stays (add,adc,adc,adc,...)

  3. on modern languages/platforms not have direct alu flag register access anymore

    sometimes can tap assembler can slower in cases computation itself. in case use approach 32bit alu in c++ cy carry flag


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -