Problem · Bit Manipulation
Invert Ad Visibility Bits
Learn this problemProblem statement
An e-commerce platform encodes the current visibility of its advertisements as a positive decimal integer base10. In the integer's binary representation, 1 means an ad is visible and 0 means it is hidden.
On every page load, invert every significant bit, beginning with the highest-order 1 bit and continuing through the rightmost bit:
- Change each
1to0. - Change each
0to1.
Convert the resulting binary value back to decimal and return it.
Implement invertAdVisibility with one parameter, int base10, and return the inverted value as an int.
Function
invertAdVisibility(base10: int) → intExamples
Example 1
base10 = 30return = 1The significant binary representation of 30 is 11110. Flipping all five bits gives 00001, whose decimal value is 1.
Example 2
base10 = 10return = 5The significant bits of 10 are 1010. Their inversion is 0101, which equals 5.
Example 3
base10 = 1return = 0The only significant bit is 1. Flipping it produces 0.
Constraints
base10is a positive integer that fits in the authoredintinterface.