aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/leetcode/cpp/371.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-26 12:50:45 +0800
committercrupest <crupest@outlook.com>2021-09-26 12:50:45 +0800
commit33fea2e2d0b0474fdba253eaa3d4c08d2ae9bedb (patch)
tree883eea95e3800b827262f98025c761d7321c5884 /works/solutions/leetcode/cpp/371.cpp
parent468279deaa02f5c73812aa015b5aa16d0204cc30 (diff)
downloadcrupest-33fea2e2d0b0474fdba253eaa3d4c08d2ae9bedb.tar.gz
crupest-33fea2e2d0b0474fdba253eaa3d4c08d2ae9bedb.tar.bz2
crupest-33fea2e2d0b0474fdba253eaa3d4c08d2ae9bedb.zip
import(solutions): ...
Diffstat (limited to 'works/solutions/leetcode/cpp/371.cpp')
-rw-r--r--works/solutions/leetcode/cpp/371.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/works/solutions/leetcode/cpp/371.cpp b/works/solutions/leetcode/cpp/371.cpp
new file mode 100644
index 0000000..3a7bc8b
--- /dev/null
+++ b/works/solutions/leetcode/cpp/371.cpp
@@ -0,0 +1,25 @@
+
+class Solution {
+public:
+ int getSum(int a, int b) {
+ unsigned x = a, y = b;
+
+ unsigned carry = 0;
+
+ unsigned result = 0;
+
+ for (int i = 0; i < sizeof(int) * 8; i++) {
+ unsigned mask = 1 << i;
+
+ unsigned n = x & mask ? 1 : 0, m = y & mask ? 1 : 0;
+
+ if (n ^ m ^ carry) {
+ result |= mask;
+ }
+
+ carry = n & m || n & carry || m & carry;
+ }
+
+ return static_cast<int>(result);
+ }
+};