"1" in a binary integer

Source: Internet
Author: User

Here, we mainly use the technique of bit arithmetic to find out the number of 1 in the binary representation of an integer or the position of some special 1.

Number of 1

In order to find the number of 1 in binary representation, there is an algorithm with a time complexity of O (n) (the number of n 1). The main point is that if the integer x is not 0, the expression (refers to the C language expression) x& (x-1) The result of the operation is the binary representation of x the rightmost 1 of the value of the elimination, if X is 0, the value of the expression is 0. Thus, if X is not 0, we can repeatedly eliminate the rightmost 1 of x by the expression x = x& (x-1) until X is 0, eliminating the number of 1.

C Language code (input is 32-bit unsigned integer)

1 int Number_of_ones (uint32_t x) {2     int 0 ; 3      while (x) {4         1); 5         result++; 6     }7     return  result; 8 }

Right-most 1 position

The rightmost 1 position is calculated from the expression x ^ (x & (X-1)), e.g., x:0x12345678, x ^ (x & (X-1)): 0x00000008

1 uint32_t Right_most_one (uint32_t x) {2     return 1 )); 3 }

Left-most 1 position

The position of the leftmost 1 is relatively complex, the following is a more straightforward algorithm.

 1  uint32_t Left_most_one (uint32_t x) {   2  uint32_t test = x & (X-1  ); 3   (test) { 4  x = test;  5  test = x & (X-1  ); 6   7  return   X;  8 } 

A more interesting algorithm is found in the Java arraydeque allocateelements source code, which is used to calculate an integer power of 2 greater than one number. We can find the position of the leftmost 1 by shifting the bitwise right one to the power of 2.

Here is the source code PS: Think about the principle of the algorithm:)

1 uint32_t Left_most_one2 (uint32_t x) {2uint32_t tmp =x;3TMP |= (TMP >>1);4TMP |= (TMP >>2);5TMP |= (TMP >>4);6TMP |= (TMP >>8);7TMP |= (TMP >> -);8tmp++;9     returnTmp? (TMP >>1) : (0x80000000);Ten}

Reference

Http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayDeque.java

"1" in a binary integer

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.