Common bits operations in Java

Source: Internet
Author: User

One, calculates the number of 1 in the binary representation of a positive

1     //number of digits in binary notation for solving positive numbers 2     private static int countbit (int num) {3         int count = 0;4 for         (; num > 0; count++) 5         {6             num &= (num-1); 7         }8         return count;9     }

Algorithm idea: Every for loop, the rightmost 1 of NUM's binary is cleared.

Why is n &= (n–1) able to erase the rightmost 1? Because from the binary point of view, N is equivalent to the lowest bit of n-1 plus 1. For example, 8 (1000) = 7 (0111) + 1 (0001), so 8 & 7 = (+) & (0111) = 0 (0000), the 8 rightmost 1 is cleared (actually the highest 1, because there is only one 8 in the binary of 1). For example, 7 (0111) = 6 (0110) + 1 (0001), so 7 & 6 = (0111) & (0110) = 6 (0110), the rightmost 7 of the binary representation of 1 (that is, the lowest bit 1) is cleared.

Reference: algorithm-Finding the number of 1 in a binary number

Two, get the first bit of a number (to determine if the first bit of a number is 0 or 1?). )Train of thought: if the first and 1 phase and the result of 1 indicate that the first bit is 1; If 0 indicates that the I bit is 0
    Gets the value of the I-bit of the integer num    private static Boolean getbit (int num, int i)    {        return (num & (1 << i)) = 0); True indicates that the first bit is 1, otherwise 0    }

1 left I bit, get a number, this number is only the first I bit 1, the other bits are 0

Num with this number, the result is either 0, or not 0. The result is a non-0 indicating that the I bit is 1, and the result is 0 that the I bit is 0

Third, set the I bit to 1Idea: The first bit with 0 or, the value does not change. The first bit with 1 or, becomes 1. Therefore, our goal is num with an i-bit value of 1, the value of the other bits is 0 of the number of phases or
    Place the value of the I bit of the integer num to 1    private static int getbit (int num, int i)    {        return (num | (1 << i));    }

Four, set the I bit to 0 (clear 0)Idea: The first and 0 and the first I bit will become 0. The other bits are the same as the 1 and the other bits remain unchanged. In this way, I'm only going to clear the first 0.
    Place the value of the I bit of the integer num to 1    private static int getbit (int num, int i)    {       int mask = ~ (1 << i);//000100
Return (num & mask);//111011 }

In short, to get the value of the I-bit, or I position 0 or 1, the overall idea is: Move 1 left I bit, and then proceed with operation or OR operation.

Transferred from: http://www.cnblogs.com/hapjin/p/5839797.html

Common bits operations in Java (RPM)

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.