Thursday, April 16, 2015

LeetCode [201] Bitwise AND of Numbers Range




Note
  • the result of AND is 1 <==> all the operands are 1s.
  • thus the problem is equivalent to finding the continuous 1s starting from the most significant position over all the operands,
  • which is also equivalent to finding the continuous 1s starting from the most significant position over m (the minimum number) and n (the maximum number). 
  • eg., for the 4 number 24, 25, 26,27, there are two continuous 1s starting from the most significant position. Thus, the result is 11000. 
                           11000, 11001, 11010, 11011

  • after the while loop (line 6-8), 
                    mask:       11111111111111111111111111111100
                    mask&m: 00000000000000000000000000011000
                    mask&n:  00000000000000000000000000011000

Ref
[1] https://leetcode.com/problems/bitwise-and-of-numbers-range/
OJ
method1

No comments:

Post a Comment