Java bit Operation 2-leetcode 201 Bitwise and of Numbers Range

Source: Internet
Author: User
Tags bitwise

In the Java bit Operation Summary-leetcode Topic blog post summarizes the Java provides the bitwise operation operator, today also encounters the Leetcode the bitwise Operation TOPIC

Given a range [M, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, Inclus Ive.

For example, given the range [5, 7], and you should return 4.

Test instructions

Given M and N, all the numbers between M and N are returned with the result (including M and N)

Problem Solving Ideas:

The topic at first glance is very simple, loop traversal m to n bitwise AND the return result is finished, but put this solution into Leetcode will find out, then we need to summarize the law.

For example the topic gives the 5,6,7 binary

0101 0110 0111

By observing, we'll see that we just look at the high numbers, and the numbers that follow are going to be about each other.

Another example

011000 0110001 0110010 0110011

We find it seems as if you look at the high K-bits of all the numbers

Another example

01100 01101 01110) 01111 10000

We found out that if the number of bits of M and N were different, it would return 0.

The rules are as follows:

Given M and N, find the same high K-bit M and N (here m and n are integer variables, then according to 32 bits, the preceding 0 is also counted as high comparison), then the remaining high K for the rest of the position 0 is the final result.

The code is as follows:

At first I chose to traverse from the low, recording the lower L-bit to be discarded

Code 1

1      Public intRangebitwiseand (intMintN) {2         if(n = =m)3             returnm;4         intH = 0;5         intL = 0;6         intMM =m;7          while(M > 0 | | n > 0) {8             if((M & 1) = = (N & 1)) {9M >>= 1;TenN >>= 1; One++h; A             } -             Else { -M >>= 1; theN >>= 1; -L + =h; -++l; -H = 0; +             } -         } +         if(h==0)return0; A         returnMM & (0x7fffffff <<l); at}

After viewing the code is actually directly from the high start to traverse to the first different bit stop can find high K bit, the code is as follows

Code 2

1      Public intRangeBitwiseAnd2 (intMintN) {2         if(m==n)returnm;3         inti;4          for(i = 31;i>0;i--){5             if((m& (1<<i))! = (n& (1<<i)))6                      Break;7         }8         returnm& (~ (1<<i+1)-1));9}

The code is a lot simpler and a lot faster than the original.

The topic again uses the shift operation in Java to record several problems in the binary that are encountered in the problem:

1. Integer x for the-X, from the binary point of view is the X bitwise negation plus 1

2. Left shift positive negative numbers are low 0, right shift positive high 0 negative complement 1, and if the use of Java unsigned right shift >>> positive and negative highs are 0

3. Using integer inversion as a binary whole can take advantage of the idea of divide and conquer similar to merge sort

(1) Integer 32 is divided into 16 and 16 first flip

(2) Flip the first 8 bits and the last 8 bits in the first 16 bits, then flip the first 16 bits and the last 8 bits in the 8 bits

。。。 And so on, need Log232 = 5 times altogether

The code is as follows:

1      Public intReversebit (intN) {2n = ((n & 0xffff0000) >>> 16) | ((N & 0x0000ffff) << 16);3n = ((n & 0xff00ff00) >>> 8) | ((N & 0x00ff00ff) << 8);4n = ((n & 0xf0f0f0f0) >>> 4) | ((N & 0x0f0f0f0f) << 4);5n = ((n & 0XCCCCCCCC) >>> 2) | ((N & 0x33333333) << 2);6n = ((n & 0xaaaaaaaa) >>> 1) | ((N & 0x55555555) << 1);7         returnN;8}

Note Here is the entire 32-bit rollover, equivalent to 32-bit upside-down look.

For example, in the 4-bit view

1110

(1) Flip the first 2 and the last 2 bits 10 11

(2) Swap the first part and the second part to get the result 01 11

Java bit Operation 2-leetcode 201 Bitwise and of Numbers Range

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.