Show problems as
Classic problem #1009EasyBit Manipulation

Complement of Base 10 Integer

Real-world version: Flipping Every Bit of a Reading
Tracked in this browser only
Read Complement of Base 10 Integer on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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