For unsigned addition, everything is simple - with unsigned addition, overflow occurs if the resulting amount is smaller (with unsigned comparison) of any of the operands. (Warren, Algorithmic Tricks)
So just look at the resulting byte (and two bytes) and compare. Sort of
unsigned short int sum(unsigned short int a, unsigned short int b) { unsigned short int s = a + b; if ((s&0xFF < a&0xFF) && (s&0xFF < b&0xFF)) cout << "Переполнение 8 бита" << endl; if ((s < a) && (s < b)) cout << "Переполнение 16 бита" << endl; return s; }
Without if'ov - Urorren gives such options as (c - transfer, ie, the sum x + y + c is considered):
(x&y)|((x|y)&~(x+y+c)) (x>>1)+(y>>1)+(((x&y)|((x|y)&c))&1)
Well, in your case, c == 0, which simplifies the case ... Complicates the fact that the test should be considered separately from the summation, and separately for bytes and words.
Update:
unsigned short int sum(unsigned short int x, unsigned short int y, bool&carry8, bool& carry16) { unsigned short int s = x+y; carry16 = ((x&y)|((x|y)&~s)); carry8 = carry16&0x80; carry16 = carry16&0x8000; return s; }
Exhaustive brute force validation. There are no shifts, if'ov no.
Arranges?