Solution for HackerRank Flipping bits

Hello Programmers,

The solution for hackerrank Flipping bits problem is given below.

Problem Link:-

*   Author:- Rahul Malhotra
*   Source:- Programming Vidya
*   Description:- Solution for HackerRank Flipping bits problem
*   Problem Link:-
*   Website:-

#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";


    return 0;

Happy Coding..!!


Leave a Reply

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

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

Facebook photo

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

Connecting to %s