Solution for HackerRank Flipping bits

Hello Programmers,

The solution for hackerrank Flipping bits problem is given below.

Problem Link:- https://www.hackerrank.com/challenges/flipping-bits/problem

/*
*   Author:- Rahul Malhotra
*   Source:- Programming Vidya
*   Description:- Solution for HackerRank Flipping bits problem
*   Problem Link:- https://www.hackerrank.com/challenges/flipping-bits/problem
*   Website:- www.programmingvidya.com
*/

#include <bits/stdc++.h>

using namespace std;

// Complete the flippingBits function below.
long flippingBits(long n) {

    // * Initializing variables
    unsigned long int mask = 1;

    // * Calculating and returning the number obtained after flipping all bits
    mask = mask<<32;
    return mask-1-n;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    int q;
    cin >> q;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    for (int q_itr = 0; q_itr < q; q_itr++) {
        long n;
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        long result = flippingBits(n);

        fout << result << "\n";
    }

    fout.close();

    return 0;
}

Happy Coding..!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s