位运算(代言人GR

发布时间 2023-11-28 00:00:05作者: Zhao_zzZ

题目传送门

题目

Description

这是一道位运算模板题。

有T组不同询问,每组询问给出 op,x,yop,x,y,表示使用第 opop 种操作对 x,yx,y 进行二进制位运算。

共有 6 种不同的 op, 涵义如下:

op=1: 将 x 右移 y 位

op=2: 将 x 左移 y 位

op=3: 将 x 的右数第 y 位(最末位为右数第1位,依此类推)强制改为 1

op=4: 将 x 的右数第 y 位强制改为 0

op=5: 将 x 的右数第 y 位取反

op=6: 截取 x 的右 y 位

Input Format

输入第一行为一个正整数 \(T\)

接下来 \(T\) 行,每行包括三个正整数 \(op,x,y\),以空格分开。

\(1\le op\le 6\)

\(0\le x\le 2^{63} - 1\)

\(1\le y\le 50\)

Output Format

输出 T 行答案(答案在 long long 范围内)。

Sample

输入

6
1 7 1
2 6 2
3 13 2
4 15 1
5 11 3
6 259 3

输出

3
24
15
14
15
3

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t;
signed main(){
	scanf("%lld",&t);
	while(t--){
		int op,x,y;
		scanf("%lld%lld%lld",&op,&x,&y);
		if(op==1) printf("%lld\n",x>>y);
		if(op==2) printf("%lld\n",x<<y);
		if(op==3) printf("%lld\n",x|(1ll<<(y-1ll)));
		if(op==4) printf("%lld\n",x&~(1ll<<(y-1ll)));
		if(op==5) printf("%lld\n",x^(1ll<<(y-1ll)));
		if(op==6) printf("%lld\n",x&(1ll<<y)-1ll);
	}
	return 0;
}