Vijos - P1201高低位交换(模拟/位运算)

屠龙宝刀点击就送

模拟不写了。

提供一些位运算巧妙方法(非原创)。

#include<cstdio>
typedef unsigned int u32;

u32 n;

inline void p(u32 x){//Debug
	for(int i=31;i>=0;i--){
		if(i%4==3)printf(" ");
		printf("%u",x&(1<<i)?1:0);
	}
	printf("\n");
}

int main(){
//	freopen("swap.in","r",stdin);
//	freopen("swap.out","w",stdout);
	scanf("%u",&n);
	/*
	p(n);
	p(n>>16);
	p((n-((n>>16)<<16)));
	p((n-((n>>16)<<16))<<16);
	p((n>>16)+((n-((n>>16)<<16))<<16));
	*/
	printf("%u\n",(n>>16)+((n-((n>>16)<<16))<<16));//我的沙茶做法
    //Notice the order between +- and >><<,add"()"
	//LevelUp: int a=n/65536,b=n%65536;printf("%d\n",b*65536+a);
	//LevelUp++: printf("%u\n",((n<<16)|(n>>16)));//unsigned int's Natural feature
}

//不知道为什么longlong位运算总会出锅,但unsignedint放心用

posted @ 2019-11-11 16:57  Y15BeTa  阅读(128)  评论(0编辑  收藏  举报