Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1026

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1026

To Carry or not to Carry

 

By Monirul Hasan Tomal, SEU Bangladesh

Timelimit: 2

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
6 9

2
15

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>

int main(void) {
   long long a, b;

   while (scanf("%lld %lld", &a, &b) == 2)
      printf("%lld\n", a^b);

   return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 6
6 9

Output

x
+
cmd
2
15

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
using namespace std;

int main(){
    unsigned long int x, y;

    while(cin >> x >> y){
        cout << (x ^ y) << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 6
6 9

Output

x
+
cmd
2
15

#3 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
       Scanner leitor = new Scanner(System.in);
    	while (leitor.hasNext()) {
    		long u = leitor.nextLong();
    		long m = leitor.nextLong();
    		System.out.println(u ^ m);
    	}
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 6
6 9

Output

x
+
cmd
2
15

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split(/\s+/g)

/** @typedef { number | bigint | string } anyNumberType */

const Long = {
	/**
	 * @param { anyNumberType } numA
	 * @param { anyNumberType } numB
	*/
	xor: (numA, numB, bits = 32) => {
		numA = BigInt.asUintN(bits, BigInt(numA))
		numB = BigInt.asUintN(bits, BigInt(numB))

		return numA ^ numB
	}
}

function main() {
	const output = []

	for (let index = 0; index  <  input.length; index += 2) {
		const v1 = input[index]
		const v2 = input[index + 1]

		if (v1 == "" || v2 == "") break

		output.push(Long.xor(v1, v2))
	}

	console.log(output.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 6
6 9

Output

x
+
cmd
2
15

#5 Code Example with Python Programming

Code - Python Programming


def Main(a, b):
  print(a^b)
if __name__ == '__main__':
  while True:
    try:
      a, b = map(int ,input().split())
    except EOFError:
      break
    Main(a, b)
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 6
6 9

Output

x
+
cmd
2
15
Advertisements

Demonstration


Previous
#1025 Beecrowd Online Judge Solution 1025 Where is the Marble? Solution in C, C++, Java, Python and C#
Next
#1030 Beecrowd Online Judge Solution 1030 Flavious Josephus Legend Solution in C, C++, Java, Python and C#