Hackerrank - Flipping bits Solution
1 min read

Hackerrank - Flipping bits Solution

Hackerrank - Flipping bits 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:

Function Description

Complete the flippingBits function in the editor below. It should return the unsigned decimal integer result.

flippingBits has the following parameter(s):

  • n: an integer

Input Format

The first line of the input contains , the number of queries.
Each of the next  lines contain an integer, , to process.

Constraints

Output Format

Output one line per element from the list with the decimal value of the resulting unsigned integer.

Sample Input 0

3
2147483647
1
0

Sample Output 0

2147483648
4294967294
4294967295

Sample Input 1

2
4
123456

Sample Output 1

4294967291
4294843839

Sample Input 2

3
0
802743475
35601423

Sample Output 2

4294967295
3492223820
4259365872

Solution in Python

def flippingBits(n):
    s = bin(n)[2:]
    t = s.maketrans("01", "10")
    s = s.translate(t)
    s = (32-len(s))*"1"+s
    return int(s,2)

for _ in range(int(input())):
    print(flippingBits(int(input())))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×