位运算

来源:互联网 发布:java多线程 无等待 编辑:程序博客网 时间:2024/06/03 15:49

位运算
~ 按位取反 0变1,1变0 & 按位与 只有一一才是一 | 按位或 有1就是1 ^ 按位异或 相同得0,不同为1 << 左移 二进制数向左移,最低位补零 >> 右移 二进制数向右移



1、把小写字母变为大写字母,清位:‘a’ & 0xDF,结果为‘A’
2、把大写字母变为小写字母,置位:‘A’ | 0x20,结果为‘a’
3、对某位取反, 某个位与1 异或即为取反( 第1 位取反):0xFF ^ 0x01,运算的结果为0xFE

4、部分乘法的化简,与2 的n 次方相乘,相当于左移n 位,例如0x02 乘以4,0x02 << 2,这里的2,表示4 = 2 的‘2’次方,结果为8
5、部分除法的化简,与2 的n 次方相除,相当于右移n 位,例如0x08 除以4,0x08 >> 2,这里的2,表示4 = 2 的‘2’次方,结果为2
6、部分求余的化简,与2 的n 次方求余,跟2 的n 次方-1 与,如15跟8 求余,相当于15 & 7,这里的7 是8-1 = 7,结果为8
7、其他乘法的化简,例如0x08 * 7 = 0x08 * (8 - 1) = (0x08 << 3) - 0x08
8、循环移位,对一个16 位的数循环左移n 位,0xXX >> (16 - n) | 0xXX << n
9、循环移位,对一个16 位的数循环右移n 位,0xXX << (16 - n) | 0xXX >> n


防御病毒

Time Limit: 1000MS Memory Limit: 1000K

Description

Z王国的网络系统遭到了来自W帝国的袭击,W帝国使用了X病毒污染了Z王国的大量数据。碰巧,Z王国的数据存储方式与地球相似,所以Z王国的国王Q派出了包身工J向来自地球的你求救。现已知,Z王国的数据均是采取32位原码存储(原码即整数的二进制码,最高位为符号位)。而X病毒的攻击方式是将数据原码的前16位与后16位交换。
例如:
-1的原码是1000000000000000 0000000000000001
经过攻击后会变成0000000000000001 1000000000000000即98304
而你所需要做的则是将数据复原。

Input

第一行输入一个整数n(1<=n<=100000),表示有n个数据需要复原。

Output

输出一个数字在一行,表示答案。

Sample Input

4
98304
-1
1
2

Sample Output
-1
98304
65536
131072

#include<iostream>using namespace std;int main(){    int n;    cin >> n;    while (n--)    {        int a;        int q, h, t;        cin >> a;        t = a;        if (t>0)        {            q = t >> 16;            h = t & 65535;            t = ((h & 32767) << 16) + q;            if (h & 32768)                t = -t;            cout << t << endl;        }        else        {            t = -t;            q = t >> 16;            q = q & 65535;            q += 32768;            h = t & 65535;            t = ((h & 32767) << 16) + q;            if (h >> 15)                t = -t;            cout << t << endl;        }    }}



异或的规律
0^x=x
x^x=0



实现两个值的交换,而不必使用临时变量。

a = a^b;b = b^aa = a^b;

e.g.
一个数列中每个数都出现了两次,但是有一个数只出现了一次,找出这个数:把所有数都异或,最后得到的结果就是这个单独出来的数

Just do it

Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)

Problem Description

There is a nonnegative integer sequence a1…n of length n. HazelFan wants to do a type of transformation called prefix-XOR, which means a1…n changes into b1…n, where bi equals to the XOR value of a1,…,ai. He will repeat it for m times, please tell him the final sequence.

input

The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
The first line contains two positive integers n,m(1≤n≤2×105,1≤m≤109).
The second line contains n nonnegative integers a1…n(0≤ai≤230−1).

Output

For each test case:
A single line contains n nonnegative integers, denoting the final sequence.

Sample Input

2
1 1
1
3 3
1 2 3

Sample Output

1
1 3 1

Source

2017 Multi-University Training Contest - Team 7


#include<stdio.h>#include<math.h>int num[200000 + 5], n, m;void cal(){    int i = 0;    while (pow(2, i + 1) <= m)i++;    int sq = (int)pow(2, i);    m -= sq;    if (sq<n) for (i = sq + 1; i <= n; i++) num[i] ^= num[i - sq];}int main(){    int t, i;    scanf("%d", &t);    while (t--) {        scanf("%d%d", &n, &m);        for (i = 1; i <= n; i++)scanf("%d", &num[i]);        while (m)cal();        for (i = 1; i <= n - 1; i++)printf("%d ", num[i]);        printf("%d\n", num[i]);    }}
原创粉丝点击