---
# System prepended metadata

title: '**作業2 程式語言十八般武藝**'

---

# **作業2 程式語言十八般武藝**

題目：Sum of Two Integers

1. C
```
int getSum(int a, int b){
    return a+b;
}
```
2. C++
```
class Solution {
    public:
        int getSum(int a, int b) {
            return a+b;
        }
};
```
3. java
```
class Solution {
    public int getSum(int a, int b) {
        return a+b;
    }
}
```
4. python
```
class Solution(object):
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        c = 0xffffffff
        sum = (a ^ b) & c
        temp = a & b
        while temp:
            a = sum
            b = (temp << 1) & c
            sum = (a ^ b) & c
            temp = a & b
        if sum & 0x80000000:
            sum -= 0x100000000
        return sum

```
5. python3
```
class Solution:
    def getSum(self, a: int, b: int) -> int:
        return sum([a,b])
```
6. csharp
```
public class Solution {
    public int GetSum(int a, int b) {
        while(b != 0){
            int temp = a & b;
            a = a ^ b;
            b = temp << 1;
        }        
        return a;
    }
}
```
7. javascript
```
var getSum = function(a, b) {
    if(b == 0) {
        return a;
    }
    return getSum(a ^ b, (a & b) << 1);
};
```
8. swift
```
class Solution {
    func getSum(_ a: Int, _ b: Int) -> Int {
        var sum = a, temp = b
        while temp != 0 {
            (sum, temp) = (sum ^ temp, (sum & temp) << 1)
        }
        return sum
    }
}
```
9. golang
```
func getSum(a int, b int) int {
    for {
        a, b = a ^ b, (a & b) << 1
        if b == 0 {
            break
        }
    }
    return a
}
```
10. php
```
class Solution {
    function getSum($a, $b) {
        while ($b != 0) {
        $carry = $a & $b;
        $a = $a ^ $b;
        $b = $carry <<1;
        }
        return $a;
    }
}
```