Invert Binary Number - aloalgo.com

Invert Binary Number

Easy
New!

You are given a non-negative integer n.

Your task is to invert the binary representation of n (changing all 0s to 1s and all 1s to 0s) and return the decimal value of the inverted binary number.

Note that:

  • You should only invert the bits that constitute the significant binary representation of n, ignoring any implicit leading zeros.

Return the decimal value of the inverted binary number.

Example 1

Input
5
Output
2
Explanation:

The number 5 in binary is 101. Inverting its bits gives 010, which is 2 in decimal.

Example 2

Input
0
Output
1
Explanation:

The number 0 in binary is 0. Inverting its bit gives 1, which is 1 in decimal.

Example 3

Input
7
Output
0
Explanation:

The number 7 in binary is 111. Inverting its bits gives 000, which is 0 in decimal.

Loading...

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!