Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Flipping bits #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions 22. Flipping Bits/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Topic : Algorithms
Subtopic : Flipping bits
Language : C++
Problem Statement :
You will be given a list of 32 bit unsigned integers. Write a function to Flip all the bits (1 -> 0 and 0 -> 1) and return the result as an unsigned integer.
Url : https://www.hackerrank.com/challenges/flipping-bits/problem
*/

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

long BITS = (long)(pow(2, 32) - 1);

/*
* Complete the 'flippingBits' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER n as parameter.
*/

long flippingBits(long n)
{
return BITS ^ 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<int, int>(isspace))));

return s;
}

string rtrim(const string &str)
{
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end());

return s;
}
44 changes: 44 additions & 0 deletions 22. Flipping Bits/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
Topic : Algorithms
Subtopic : Flipping bits
Language : Python3
Problem Statement :
You will be given a list of 32 bit unsigned integers. Write a function to Flip all the bits (1 -> 0 and 0 -> 1) and return the result as an unsigned integer.
Url : https://www.hackerrank.com/challenges/flipping-bits/problem
'''
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'flippingBits' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts LONG_INTEGER n as parameter.
#

BITS = pow(2, 32) - 1


def flippingBits(n):
# Write your code here
return BITS ^ n


if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

q = int(input().strip())

for q_itr in range(q):
n = int(input().strip())

result = flippingBits(n)

fptr.write(str(result) + '\n')

fptr.close()