Algorithm
To Carry or not to Carry
By Monirul Hasan Tomal, SEU Bangladesh
6+9=15 seems okay. But how come 4+6=2?
Look at, Mofiz worked hard throughout his Digital Logic course, but when he was asked to implement a 32 bit adder for the laboratory exam, he did some mistake in the design part. After scavenging the design for half an hour, he found his flaw!! He was doing bitwise addition but his carry bit always had zero output. Thus,
4 = 00000000 00000000 00000000 00000100
+6 = 00000000 00000000 00000000 00000110
----------------------------------------
2 = 00000000 00000000 00000000 00000010
It’s a good thing that he finally found his mistake, but it was too late. Considering his effort throughout the course, the instructor gave him one more chance. Mofiz had to write an efficient program that would take 2 unsigned 32 bit decimal numbers as input, and produce an unsigned 32 bit decimal number as the output adding in the same way as his circuit does
Input
In each input line there will be a pair of integer separated by a single space. Input ends at EOF.
Output
For each input line, the program must provide an output's line: the value after adding the two numbers in the "Mofiz way"
Input Sample | Output Sample |
4 6 |
2 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main()
{
int unsigned long a,b,c;
while(scanf("%lu %lu",&a,&b)==) {
c = (a^b);
printf("%lu\n" ,c);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main()
{
unsigned long int x, y, r;
while(cin >> x >> y)
{
r = x ^ y;
cout << r << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOExeception {
Scanner sc = new Scanner(System.in);
long x, y, soma;
while (sc.hasNext()) {
x = sc,nextLong();
y = sc.nextLong();
soma - x^y;
System.out.println(soma);
}
}
}
Copy The Code &
Try With Live Editor
Input
Output