From 7612fc31db53002d63b3038eb0685fcb920edd92 Mon Sep 17 00:00:00 2001 From: pankajkr987 <94712990+pankajkr987@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:43:40 +0530 Subject: [PATCH] Create Flipping Bits You will be given a list of 32 bit unsigned integers. Flip all the bits ( and ) and return the result as an unsigned integer. Example: sample input: 3 2147483647 1 sample output: 2147483648 4294967294 4294967295 0 --- Flipping Bits | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Flipping Bits diff --git a/Flipping Bits b/Flipping Bits new file mode 100644 index 0000000..2fe62dc --- /dev/null +++ b/Flipping Bits @@ -0,0 +1,59 @@ +#include + +using namespace std; + +string ltrim(const string &); +string rtrim(const string &); + + +long flippingBits(long n) { +return unsigned(~n); +} + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + string q_temp; + getline(cin, q_temp); + + int q = stoi(ltrim(rtrim(q_temp))); + + for (int q_itr = 0; q_itr < q; q_itr++) { + string n_temp; + getline(cin, n_temp); + + long n = stol(ltrim(rtrim(n_temp))); + + long result = flippingBits(n); + + fout << result << "\n"; + } + + fout.close(); + + return 0; +} + +string ltrim(const string &str) { + string s(str); + + s.erase( + s.begin(), + find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) + ); + + return s; +} + +string rtrim(const string &str) { + string s(str); + + s.erase( + find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), + s.end() + ); + + return s; +} +