Bit operation considerations

Source: Internet
Author: User

Gradual update ...

1. Bitwise operators have low priority, so use parentheses to ensure the order of operations, otherwise it is likely to get the results. For example, to get numbers like 1,3,5,9 these 2^i+1. write int a = 1 << i + 1; The program executes the i + 1 First and then the left shift operation. should be written as int a = (1 << i) + 1;

2. Parity judgment

for (i = 0; i < ++i)//output 0~100 all odd if (I & 1)    printf ("%d", i);p utchar (' \ n ');

3. No third-party variable exchange two number

void Swap (int &a, int &b) {if (A! = b)//is required here, otherwise it will a^b==0, the result is a, B is used to exchange, after this function operation, all become 0 {a ^= b;b ^= a;a ^= b;}}

4. Transform Symbols

Take the reverse plus one

int Sign_rev (int a) {    return ~a+1;}

5. Seek absolute value

int my_abs (int a) {int i=a>>31;//If A is positive, then I is 0, if a is negative, then I is -1,0 xor or a number is a number,-1 XOR or a number, equals a number to take the inverse return (i^a)-I;

6. Reverse Binary

The first step: each 2 bits is a group, in the group high and low bit exchange

10 00 01 10 11 01 10 00

-->01 00 10 01 11 10 01 00

The second step: every 4 bits is a group, in the group high and low bit exchange

0100 1001 1110 0100

-->0001 0110 1011 0001

The third step: every 8 bits for a group, in the group of high and low bit exchange

00010110 10110001

-->01100001 00011011

Fourth step: Each 16 bits is a group, in the group high and low bit exchange

01100001 00011011

-->00011011 01100001

#include <stdio.h>void print (int a) {    int i;    For (I=sizeof (a) *8-1;i>=0;--i)    {        if ((a>>i) &1)            Putchar (' 1 ');        else            Putchar (' 0 ');        if (i%8==0)            Putchar (");    }    Putchar (' \ n ');} int main (int argc, char *argv[]) {    unsigned int a=34520;    Print (a);    A= ((A&0XAAAAAAAA) >>1) | ((a&0x55555555) <<1);    Print (a);    A = ((A & 0xCCCCCCCC) >> 2) | ((A & 0x33333333) << 2);    Print (a);    A = ((A & 0xf0f0f0f0) >> 4) | ((A & 0x0f0f0f0f) << 4);    Print (a);    A = ((A & 0xff00ff00) >> 8) | ((A & 0x00ff00ff) << 8);    Print (a);    unsigned int b=0;    for (short int i=1;i!=0;i<<=1)    {        b<<=1;        if (a&1)            b|=1;        a>>=1;    }    Print (b);    return 0;}

7. Number of 1 in binary

1) First write a method that derives from the tree array:

int cnt=0;    while (b) {        b-= (b&-b);        cnt++;    } printf ("CNT is%d\n", CNT);
The last 1 of the binary is subtracted, and the last one can be reduced to a total of several times, that is, how many 1. The number of operations is independent of the size of the input n and is only related to the number of 1 in N. If the binary representation of N has K 1, then this method only needs to loop K times to

2)

the speed is not necessarily the quickest, but the idea is absolutely ingenious. To say the secret, in fact, it is very simple, first n is written in binary form, and then the adjacent bits add, repeat the process, until only one left. Take 217 (11011001) For example, there is a picture of the truth, the following figure is enough to explain everything. The binary representation of 217 has 5 1:


int BitCount4 (unsigned int n) {     n = (n &0x55555555) + ((n >>1) &0x55555555);     n = (n &0x33333333) + ((n >>2) &0x33333333);     n = (n &0x0f0f0f0f) + ((n >>4) &0x0f0f0f0f);     n = (n &0x00ff00ff) + ((n >>8) &0x00ff00ff);     n = (n &0x0000ffff) + ((n >>16) &0x0000ffff);     return n; }
8. The missing digit Leetcode the single number

There must be a number that appears only once and the other numbers appear even several times. It is not necessary to use the search to do, the use of the two characteristics of the XOR operation--1. Oneself or the result is 0,2. Different or satisfy the Exchange law. So we're going to have these numbers all different or again, and the result must be the number that only appears.

const int MAXN = 15;int Data[maxn] = {1, 347, 6, 9, 889, 712, 889, 1, 9,,, 347 712};int = Lostnum (in t i = 0; i < MAXN; i++) lostnum ^= data[i];p rintf ("Missing number:  %d\n", lostnum);




Bit operation considerations

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.