【C语言】学习笔记--单目操作符有哪些呢?(2)

~ 按位取反操作

#include <stdio.h>

int main()
{
	//按位取反操作
	/*
	* 按位取反操作,得到负数-1
		对负数进行打印:
		由补码--》反码--》原码打印出来的是原码
	*/
	int a = 0;
	//00000000000000000000000000000000
	//11111111111111111111111111111111 - 补码
	//11111111111111111111111111111110 - 反码
	//10000000000000000000000000000001 - 原码
	//-1
	printf("%d\n", ~a);
	
	return 0;
}
#include <stdio.h>

int main()
{
	//按位取反的运用场景

	//把11的二进制		000000000000000000000000000000000000001011
	//改成15的二级制	000000000000000000000000000000000000000001
	int a = 11;
	a = a | (1 << 2);
	printf("%d\n", a);
	//解析:第二步是1向左移两个。然后最右补回两个0,变成...0100
	//a = 11:		000000000000000000000000000000000000001011
	//1:			000000000000000000000000000000000000000001
	//1 << 2:		000000000000000000000000000000000000000100
	//a | (a << 2)	000000000000000000000000000000000000001111

	return 0;
}
#include <stdio.h>

int main()
{
	int a = 15;
	//把15的二级制		000000000000000000000000000000000000000001
	//改回11的二进制	000000000000000000000000000000000000001011
	a = a & (~(1 << 2));
	printf("%d\n", a);//11
	//解析:第二步:(~(1 << 2))将1右移两个, 最左用O补齐, 最后按位取反
	//a = 15			000000000000000000000000000000000000001011
	//1:				000000000000000000000000000000000000000001
	//1 << 2:			000000000000000000000000000000000000000100
	// ~(1 << 2):		111111111111111111111111111111111111111011
	//a &  ~(1 << 2):	000000000000000000000000000000000000001011

	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页