All problems
0873EasyBit Manipulation

Flipping Every Bit of a Reading

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1009Complement of Base 10 Integer

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A meter reading n is held in binary with no leading zeros, so the reading 5 is held as 101.

Flipping the reading turns every 0 into a 1 and every 1 into a 0, across exactly the bits the reading is held in. Flipping 101 gives 010, which is the reading 2.

Return the reading that flipping produces. The reading 0 is held as the single bit 0, so flipping it gives 1.

Examples

Example 1

Input
n = 2
Output
1

The reading 2 is held as 10. Flipping every bit gives 01, which is the reading 1.

Example 2

Input
n = 8
Output
7

The reading 8 is held as 1000, and flipping every bit gives 0111.

Example 3

Input
n = 0
Output
1

The reading 0 is held as the single bit 0, and flipping that bit gives 1.

Constraints

  • 0 <= n <= 999999999

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def flip_reading(n: int) -> int:
Java
public int flipReading(int n)
September 7
Apply