"Bit operations you don't know can still be used like this ."

Source: Internet
Author: User

"Bit operations you don't know can still be used like this ."
1. Calculate the number of 1 in the binary representation of a number

After a decimal number is reduced by 1, the 1 position of its decimal digit changes to 0, and the next position changes to 1. Therefore, it changes the 1 position of its decimal digit to 0. (For example, the Binary Expression of Number 6 is 00000110, And the Binary Expression of number 5 is 00000101. After performing an operation, the two digits change to 00000100 after the two digits are in phase, the number of 1 in the Binary Expression of Number 6 is reduced by one)

int countOf_1(int num){    int ans = 0;    while(num){        num &= num - 1;        ans++;    }    return ans;}



2. Determine whether a number is the N power of 2.

Determine whether a number is the N power of 2, that is, to determine whether the highest bit of a binary number is 1, and only one

bool is_2Power(int num){    num &= num - 1;    return num == 0;}



3. How many times can integer n be changed to m (the number of binary digits changed)
Int countChange (int n, int m) {return countOf_1 (n ^ m); // call the first function}



4. obtain the maximum and minimum int values.
Pair <int, int> getLimitInt () {pair <int, int> p; p. first = ~ (1 <31); // int maximum value p. second = 1 <31; // int minimum value return p ;}



5. Judge the parity of a number

To determine whether a number is an odd number, you can determine whether the second bit in the Binary Expression of a number is 1.

bool isOdd(int num){    return num & 1 == 1;}



6. Exchange the values of two numbers

In decimal, the values of two numbers a and B can be as follows: a = a + B; B = a-B; a = a-B; similarly, in binary format, you can use an exclusive or operation: a = a ^ B; B = a ^ B; a = a ^ B;

void swap(int a, int b){    a ^= b ^= a ^= b;    return ;}



7. Calculate the average value of two numbers
int getAverage(int m, int n){    return (m + n) >> 1;}



8. Calculate the m-digit reciprocal Value
int getMthN(int n, int m){    return (n >> (m - 1)) & 1;}



9. Set the m-digit to 1.
void setMthN_1(int n, int m){    n |= (1 << (m - 1));    return ;}



10. Set the MTH digit to 0.

void setMthN_0(int n, int m){    n &= ~(1 << (m - 1));    return ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.