Problem · Bit Manipulation

Invert Ad Visibility Bits

Learn this problem
EasyJPMorgan Chase logoJPMorgan ChaseINTERNOA

Problem 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 1 to 0.
  • Change each 0 to 1.

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) → int

Examples

Example 1

base10 = 30return = 1

The significant binary representation of 30 is 11110. Flipping all five bits gives 00001, whose decimal value is 1.

Example 2

base10 = 10return = 5

The significant bits of 10 are 1010. Their inversion is 0101, which equals 5.

Example 3

base10 = 1return = 0

The only significant bit is 1. Flipping it produces 0.

Constraints

  • base10 is a positive integer that fits in the authored int interface.

More JPMorgan Chase problems

drafts saved locally
public int invertAdVisibility(int base10) {
  // Write your code here.
}
base1030
expected1
checking account