5

I am new to programming. I took a basic course in Python, so I know the basic. I am trying to practice a lot more. I am attempting this question and I don't know where to start.

You will be given a list of 32-bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. unset bits must be set, and set bits must be unset).

Input Format

The first line of the input contains the list size T. T lines follow each line having an integer from the list.

Constraints

1 ≤ T ≤ 100

Output Format

Output one line per element from the list with the requested result.

Sample Input

3 2147483647 1 0

Sample Output

2147483648 4294967294 4294967295

Explanation

Take 1 for example, as unsigned 32-bits is 00000000000000000000000000000001 and doing the flipping we get 11111111111111111111111111111110 which in turn is 4294967294

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
sanster9292
  • 1,146
  • 2
  • 9
  • 25

2 Answers2

14

Can be done with the bitwise XOR operator, which is ^ in Python.

Example:

a = 0xF0101010
b = 0xFFFFFFFF
print(bin(a))
print(bin(b))
print(bin(a ^ b))

0b11110000000100000001000000010000
0b11111111111111111111111111111111
0b1111111011111110111111101111
wvdz
  • 16,251
  • 4
  • 53
  • 90
10
foreach x in input:
    x_flipped = ~x & 0xffffffff
    print "bits flipped in unsigned 32-bit", bin(x_flipped)

Explained: - (~x) flips all bits ~ (& 0xffffffff) converts 2's complement into unsigned int.

sramij
  • 4,775
  • 5
  • 33
  • 55
  • is this the way that you were suggesting? def flipflop(bits): the_bit = raw_input("") bit_flipped = ~(the_bit) & 0xffffffff print bin(bit_flipped) I am getting a traceback error that says that bit_flipped is not defined – sanster9292 Jan 15 '15 at 08:06