✔️✔️✔️easy solution w/ detailed explanation & dry run - C++ || JAVA⚡⚡⚡beats 100%⚡⚡⚡
Code\nC++ []\nclass Solution {\npublic:\n    int sum(int a, int b) {\n        while(b != 0){\n            int carry = a & b;\n            a = a ^ b;\n          
# Code\n```C++ []\nclass Solution {\npublic:\n    int sum(int a, int b) {\n        while(b != 0){\n            int carry = a & b;\n            a = a ^ b;\n            b = carry << 1;\n        }\n        return a;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int getSum(int a, int b) {\n        while(b != 0){\n            int carry = a & b;\n            a = a ^ b;\n            b = carry << 1;\n        }\n        return a;\n    }\n}\n```\n\n# Step-by-Step Explanation:\n**Initialization**:\n    - `carry` is initially set to 0.\n    - The loop starts with `b` not equal to 0.\n\n**Loop Execution**:\n- Inside the loop:\n1) `carry` is calculated as the bitwise AND (`&`) of `a` and `b`.\n2) `a` is updated by performing bitwise XOR (`^`) between `a` and `b`.\n3) `b` is updated by shifting the `carry` one position to the left (`<< 1`).\n4) The loop continues until `b` becomes 0.\n\n**Termination**:\n    - When `b` becomes 0, the loop exits.\n    - The final value of `a` represents the sum of the original `a` and `b`.\n\n# Dry Run for Input: `a = 12`, `b = 5`\n1. **Initial Values**:\n    - `a = 12` (binary: `1100`)\n    - `b = 5` (binary: `0101`)\n    - `carry = 0`\n\n2. **First Iteration**:\n    - `carry = a & b = 4` (binary: `0100`)\n    - `a = a ^ b = 9` (binary: `1001`)\n    - `b = carry << 1 = 8` (binary: `1000`)\n\n3. **Second Iteration**:\n    - `carry = a & b = 8` (binary: `1000`)\n    - `a = a ^ b = 1` (binary: `0001`)\n    - `b = carry << 1 = 16` (binary: `10000`)\n\n4. **Third Iteration**:\n    - `carry = a & b = 0`\n    - `a = a ^ b = 17` (binary: `10001`)\n    - `b = 0`\n\n5. **Result**:\n    - `a` after the loop: 17 (decimal)\n-------------------------------------------------------------\n-------------------------\n----------------------------------------------------\nLet\'s understand some key elements from this code in detail.\n# 1. Bitwise AND (`&`):\n- The `&` operator compares corresponding bits of two numbers.\n- If both bits are 1, the result bit is 1; otherwise, it\'s 0.\n- Example:\n    ```\n      1100 (12 in binary)\n  & 0101 (5 in binary)\n  ---------------\n      0100 (4 in binary)\n    ```\n    - The common set bit is at the second position (from the right), which corresponds to the value 4.\n\n# 2. Bitwise XOR (`^`):\n- The `^` operator compares corresponding bits of two numbers.\n- If the bits are different (one is 0 and the other is 1), the result bit is 1; otherwise, it\'s 0.\n- Example:\n    ```\n      1100 (12 in binary)\n  ^ 0101 (5 in binary)\n  ---------------\n    1001 (9 in binary)\n    ```\n    - The XOR operation adds the bits without considering the carry.\n\n# 3. Left Shift (`<<`):\n- The `<<` operator shifts the bits of a number to the left.\n- It effectively multiplies the number by 2 raised to the specified shift count.\n- Example:\n    ```\n    0100 (4 in binary) << 1\n    -----------------------\n    1000 (8 in binary)\n    ```\n    - Shifting 4 one position to the left results in 8.\n\n# Visual Representation:\nLet\'s visualize these operations using charts:\n\n1. **Bitwise AND (`&`)**:\n    ```\n    a: 1100\n    b: 0101\n    ------------\n    result: 0100 (carry)\n    ```\n\n2. **Bitwise XOR (`^`)**:\n    ```\n    a: 1100\n    b: 0101\n    ------------\n    result: 1001\n    ```\n\n3. **Left Shift (`<< 1`)**:\n    ```\n    b: 0100\n    ------------\n    result: 1000\n    ```
['Math', 'Bit Manipulation', 'C++', 'Java', 'Python3']