Tuesday, October 27, 2020

Flipping bits - Hacker Rank Solution

 You will be given a list of 32 bit unsigned integers. Flip all the bits ( and ) and print the result as an unsigned integer.

For example, your decimal input . We're working with 32 bits, so:



Flipping bits  - Hacker Rank Solution

C++

#include <cstdio>
using namespace std;

int main() {
    register int N;
    unsigned int x;
    for (scanf("%d", &N); N; --N) {
        scanf("%u", &x);
        printf("%u\n", ~x);
    }
    return 0;
}

Python 2

#Another solution
for _ in range(input()):
    s = 2**32 ^ int(raw_input())
    t = str(bin(s))[2:]
    t = t.replace('0','2')
    t = t.replace('1','0')
    t = t.replace('2','1')
    print int(t,2)

No comments:

Post a Comment

Powered by Blogger.