Swift Solution Based on Knowledge from Wikipedia
https://en.wikipedia.org/wiki/Negative_base (see Calculation part)\n\nBe careful for -5 / -2 = 3 mod 1 case (because -2 * 3 + 1 = -5). However, if we do -5 / -2
https://en.wikipedia.org/wiki/Negative_base (see `Calculation` part)\n\nBe careful for `-5 / -2 = 3 mod 1` case (because `-2 * 3 + 1 = -5`). However, if we do `-5 / -2` in our program, it will result in `2` (closer to 0). So we use `(N - 1) / -2` here for such cases.\n\n```swift\nclass Solution {\n    func baseNeg2(_ N: Int) -> String {\n        var N = N, result = ""\n        while N != 0 {\n            result = String(abs(N % -2)) + result\n            if N > 0 {\n                N /= -2\n            } else {\n                // N = Int(ceil(Double(N) / -2))\n                N = (N - 1) / -2\n            }\n        }\n        return result.isEmpty ? "0" : result\n    }\n}\n```\n\nThus, we simplify the code as below:\n```swift\nclass Solution {\n    func baseNeg2(_ N: Int) -> String {\n        var N = N, result = ""\n        while N != 0 {\n            result = String(abs(N % -2)) + result\n            N = Int(ceil(Double(N) / -2))\n        }\n        return result.isEmpty ? "0" : result\n    }\n}\n```\n\nIf we change this to binary operation, it will become the same version of `lee215`\'s solution described in https://leetcode.com/problems/convert-to-base-2/discuss/265507/JavaC++Python-2-lines-Exactly-Same-as-Base-2 :\n\n```swift\nclass Solution {\n    func baseNeg2(_ N: Int) -> String {\n        var N = N, result = ""\n        while N != 0 {\n            result = String(N & 1) + result\n            N = -(N >> 1)\n        }\n        return result.isEmpty ? "0" : result\n    }\n}\n```
[]