算法讲解-左神【1】 二进制和位运算

本文介绍了将正常数转换为计算机表示的二进制形式,包括正数处理、减一、取反,以及将计算机表示的二进制数转换回正常数的过程,涉及取反、加一和判断正负。同时讲解了C++代码示例中整型数值的二进制表示和基本运算操作。
摘要由CSDN通过智能技术生成

概念

由正常的数到计算机表示数的转化过程:转成正数、减一、取反。

-1 ----> 0001 ---->0000----->1111

-2 ----> 0010 ---->0001----->1110

-7 ----> 0111 ---->0110----->1001

-8 ----> 1000 ---->0111----->1000

由计算机表示的数到正常的数的过程:取反、加一、判断正负。

1001---->0110---->01111---->-7

1000---->0111---->1000---->-8  

#include <iostream>
#include <iomanip>
using namespace std;

// 本文件的实现是用int来举例的
// 对于long类型完全同理
// 不过要注意,如果是long类型的数字num,有64位
// num & (1 << 48),这种写法不对
// 因为1是一个int类型,只有32位,所以(1 << 48)早就溢出了,所以无意义
// 应该写成 : num & (1L << 48)


void printBinary(int num) {
	for (int i = 31; i >= 0; i--) {
		// 下面这句写法,可以改成 :
		// std::cout << ((num & (1 << i)) != 0 ? "1" : "0");
		// 但不可以改成 :
		// std::cout << ((num & (1 << i)) == 1 ? "1" : "0");
		// 因为num如果第i位有1,那么(num & (1 << i))是2的i次方,而不一定是1
		// 比如,num = 0010011
		// num的第0位是1,第1位是1,第4位是1
		// (num & (1<<4)) == 16(不是1),说明num的第4位是1状态
		std::cout << ((num & (1 << i)) == 0 ? "0" : "1");
	}
	std::cout << std::endl;
}

bool returnTrue() {
	cout << "进入了returnTrue函数" << endl;
	return true;
}

bool returnFalse() {
	cout << "进入了returnFalse函数" << endl;
	return false;
}

int main() {
	//非负数
	int a = 78;
	cout << a << endl;
	printBinary(a);
	cout << "===a===" << endl;
	cout << endl;
	
	//负数
	int b = -6;
	cout << b << endl;
	printBinary(b);
	cout << "===b===" << endl;
	cout << endl;

	//直接写二进制的形式定义变量
	int c = 0b1001110;
	cout << c << endl;
	printBinary(c);
	cout << "===c===" << endl;
	cout << endl;

	//直接写十六进制的形式定义变量
	// 0100 -> 4
	// 1110 -> e
	// 0x4e -> 01001110
	int d = 0x4e;
	cout << d << endl;
	printBinary(d);
	cout << "===d===" << endl;
	cout << endl;

	//~相反数
	cout << a << endl;
	printBinary(a);
	printBinary(~a);
	int e = ~a + 1;
	cout << e << endl;
	printBinary(e);
	cout << "===e===" << endl;
	cout << endl;

	// int、long的最小值,取相反数、绝对值,都是自己
	int f = std::numeric_limits<int>::min();
	std::cout << f << std::endl;
	printBinary(f);
	std::cout << -f << std::endl;
	printBinary(-f);
	std::cout << ~f + 1 << std::endl;
	printBinary(~f + 1);
	cout << "===f===" << endl;
	cout << endl;

	// | & ^
	int g = 0b0001010;
	int h = 0b0001100;
	printBinary(g | h);
	printBinary(g & h);
	printBinary(g ^ h);
	cout << "===g、h===" << endl;
	cout << endl;

	// 可以这么写 : int num = 3231 | 6434;
	// 可以这么写 : int num = 3231 & 6434;
	// 不能这么写 : int num = 3231 || 6434;
	// 不能这么写 : int num = 3231 && 6434;
	// 因为 ||、&& 是 逻辑或、逻辑与,只能连接boolean类型
	// 不仅如此,|、& 连接的两侧一定都会计算
	// 而 ||、&& 有穿透性的特点
	cout << "test1测试开始" << endl;
	bool test1 = (returnTrue() | returnFalse());
	cout << "test1结果," << test1 <<endl;
	cout << "test2测试开始" << endl;
	bool test2 = (returnTrue() || returnFalse());
	cout << "test2结果," << test2 << endl;
	cout << "test3测试开始" << endl;
	bool test3 = (returnFalse() & returnTrue());
	cout << "test3结果," << test3 << endl;
	cout << "test4测试开始" << endl;
	bool test4 = (returnFalse() && returnTrue());
	cout << "test4结果," << test4 << endl;
	
	// <<
	int i = 0b0011010;
	printBinary(i);
	printBinary(i << 1);
	printBinary(i << 2);
	printBinary(i << 3);
	cout << "===i << ===" << endl;
	
	// 非负数 >> >>>,效果一样
	// 非负数 << 1,等同于乘以2
	// 非负数 << 2,等同于乘以4
	// 非负数 << 3,等同于乘以8
	// 非负数 << i,等同于乘以2的i次方
	// ...
	// 非负数 >> 1,等同于除以2
	// 非负数 >> 2,等同于除以4
	// 非负数 >> 3,等同于除以8
	// 非负数 >> i,等同于除以2的i次方
	// 只有非负数符合这个特征,负数不要用
	int k = 10;
	cout << k << endl;
	cout << (k << 1) << endl;
	cout << (k << 2) << endl;
	cout << (k << 3) << endl;
	cout << (k >> 1) << endl;
	cout << (k >> 2) << endl;
	cout << (k >> 2) << endl;
	cout << "== = k == = " << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值