What is the simples way to check if the sum of two unsigned integers has resulted in an overflow.
Try this
int uadd32(uint32 a,uint32 b) { unsigned long long x=(unsigned long long) a+b; if (x>0xffffffff) return 0; // 0 means overflow return 1; // 1 means no overflow }
May not be fastest solution - a = b+c if a is truncated because of overflow the c != a-b
Code
a = b + c; if (c == (a-b)) printf("No Overflow"); else printf("Overflow");
Write a program to reverse the bits of an unsigned integer ??
Divide the array in two subarrays of n/2 sizes each such that the difference of the sum of two subsets is as minimum as possible. If n is even, then sizes of two subarray must be n/2 and if n is odd, then size of one subarray must be (n-1)/2 and size of other subset must be (n+1)/2.
Say you are given
int a[ ] = {1,2,3,4}; int b[ ] = {4,3,2,1}; int S = 5;
Now the task is to find all pairs of two number which adds up to S (One number from a and other number from b).