Tuesday, April 26, 2016

URI Online Judge Solution 1026 in C C++ and java || To Carry or not to Carry

Main Link: https://www.urionlinejudge.com.br/judge/en/problems/view/1026

URI Online Judge | 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 SampleOutput Sample
4 6
6 9
2
15


Details about the URI 1026 Problem solution:


URI 1026 problem is a simple problem of bitwise XOR( ^ ). For more to know about functionality of ( ^ ). You can please go here:
https://en.wikipedia.org/wiki/Bitwise_operations_in_C
Or http://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
Or See below about XOR (^):

Bitwise XOR "^"

bit abit ba ^ b (a XOR b)
000
011
101
110
The Bitwise XOR (exclusive or) performs a logical XOR function, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones.[3] XOR can be used to toggle the bits between 1 and 0. Thus i = i ^ 1 when used in a loop toggles its values between 1 and 0.[4]
After solve this check the result instead of input :

4 6
6 9
3 5
10 20
100 55
1000 1400
255 255
32000 31000
31234 255
1234 1234
1010 2020

Result:

2
15
6
30
83
1680
0
1048
31485
0
1046

URI Solution 1026 in C language:

// Problem : URI Problem No : 1026 || To carry or not to carry// Author  : urisolve.blogspot.com || Maniruzzaman Akash
#include<stdio.h>
int main()
{
   int unsigned long  a,b,c;

   while(scanf("%lu %lu",&a,&b)==2){
   c = (a^b);
printf("%lu\n",c);
}
    return 0;
}

URI Solution 1026 in C language:

// Problem : URI Problem No : 1026 || To carry or not to carry// Author  : urisolve.blogspot.com || Maniruzzaman Akash 
#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; 
}

URI Solution 1026 in Java language:(submitted and accepted verify)

// Problem : URI Problem No : 1026 || To carry or not to carry// Author  : urisolve.blogspot.com || Maniruzzaman Akash
package uri_java_problem_solution;
import java.util.Scanner;
public class URI_1026 { 
    public static void main(String[] args){ 
    Scanner sc = new Scanner(System.in);
    long x, y, ans;  
  while (sc.hasNext()) {  
        x = sc.nextLong(); 
        y = sc.nextLong(); 
        ans = x^y; 
        System.out.println(ans); 
        } 
    }
}








1 comment:
Write comments
  1. Muito bom seu blog e suas soluções, eu nao conhecia essa função e graças a voce, eu aprendi algo a mais hoje. Continue assim meu caro! =D

    ReplyDelete

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.