forked from lunchScreen/Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilpping bits.swift
30 lines (23 loc) · 878 Bytes
/
Filpping bits.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Foundation
/*
* Complete the 'flippingBits' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER n as parameter.
*/
func flippingBits(n: Int) -> Int {
let int32Max = 1<<32 - 1
return int32Max - n
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let q = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for qItr in 1...q {
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
let result = flippingBits(n: n)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
}