Check if bits in Range L to R of Two Numbers are Complement of Each other or Not

Let's assume we have two non-negative numbers, x and y, as well as two values, l and r. We have to determine whether or not all bits in the range l to r of both given numbers are complements of one another.

We will learn how to determine whether the bits in the range L to R of two numbers are complements in this article.

Examples:

Check if bits in Range L to R of Two Numbers are Complement of Each other or Not

The bits are numbered from right to left, beginning with the least significant bit.

The steps to solving the problem are outlined below.

  • Obtain xor_value = x ^ y.
  • Determine whether or not all of the bits in the range l to r of the xor value are set.

The above strategy is carried out as follows.

C++ Program:

Output:

Yes
  • Time complexity will be O(1).

Because it performs constant operations.

  • Auxiliary space will be O(1).





Latest Courses