复习笔记6 位运算 2进制数据的操作

public class Test7
{
	public static void main(String[] args)
	{
		// & 按位与
		//看这行像什么,像逻辑运算符哪里我们写的吧
		//0为假,1为真,那么结论就是第四行为1
		//而在运算方面,我们可以根据这个规则进行
		//逐位运算,即同为1为1,否则为0
		System.out.println(0 & 0);
		System.out.println(0 & 1);
		System.out.println(1 & 0);
		System.out.println(1 & 1);
		System.out.println("======================================>");
		//可以明确的看到只有最后1位都为1吧,结果就是1
		System.out.println(getIntegerToBinary(3).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(3 & 5).substring(24));
		System.out.println("======================================>");
		//在观察这个我们发现一点有趣的现象?
		System.out.println(getIntegerToBinary(7).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(7 & 5).substring(24));
		System.out.println("======================================>");
		//一排1也就是-1 & 任何数,结果还是他自己
		//可以发现所有任意数为1的位都和-1中的该位
		//重合
		//可以把这个理解成数学中的求交集
		System.out.println(getIntegerToBinary(-1));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(-1 & 1644687));
		System.out.println("======================================>");
		//任何数和0 进行&操作都会得到0,这个操作经常用来清0
		//而在数学中,任何集合都不可能和一个空集产生交集
		System.out.println(getIntegerToBinary(0));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(0 & 0));
		System.out.println("======================================>");
		//这里可以看到,0x1f是前边我们讲的拿一个数最后5位,
		//而这个0xaaaaaaaa,最后五位01010,前边数据被抛弃
		//这里可以很简单的综合上边的结论,0x1f的5位前都是0吧?
		//任何数跟0 & 都是0,而后边是不是跟咱们的-1那个很像?
		//而这个数跟1排1 & 还是自己吧?
		//这个操作我们叫做取指定位
		System.out.println(getIntegerToBinary(0x1f));
		System.out.println(getIntegerToBinary(0xaaaaaaaa));
		System.out.println(getIntegerToBinary(0x1f & 0xaaaaaaaa));
		System.out.println("======================================>");
		//0xaaaaaaaa 中间的0不太好看拿了多少位,所以用-1
		System.out.println(getIntegerToBinary(0xffffffff));
		//取后四位
		System.out.println(getIntegerToBinary(0xf & 0xffffffff));
		//取前四位
		System.out.println(getIntegerToBinary(0xf0000000 & 0xffffffff));
		//同时取前四位和后四位,至于怎么玩,那是你自己的事情。
		//在这里我们用的是16进制,那么16进制表示的是4位2进制位
		//表示16进制的1位,所以我们只需要写8位。
		//如果觉得16进制的4位有点大,可以考虑用8进制,但是不好用
		//一般人都不认识,再说位不是整除的32/3你还少1位,蛋疼
		System.out.println(getIntegerToBinary(0xf000000f & 0xffffffff));
		System.out.println("======================================>");
		
		// | 按位或
		//有一个为1,就为1,看着就懂,不多说
		//这个运算符可以当成数学中的求并集
		System.out.println(0 | 0);
		System.out.println(0 | 1);
		System.out.println(1 | 0);
		System.out.println(1 | 1);
		System.out.println("======================================>");
		//可以看到,结果是111,这就是 | 的用法,低3位都有1出现,所以是111
		System.out.println(getIntegerToBinary(3).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(3 | 5).substring(24));
		System.out.println("======================================>");
		//有趣的现象又来了,呵呵
		System.out.println(getIntegerToBinary(7).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(7 | 5).substring(24));
		System.out.println("======================================>");
		//-1 | 上任何数,结果还是-1,只能说-1这个集合里涵盖了你的任何组合
		System.out.println(getIntegerToBinary(-1));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(-1 | 1644687));
		System.out.println("======================================>");
		//0 | 上任何数,结果还是这个数,这个操作没什么太多实际意义
		//但如果是开发中,发现|操作的值没有改变,你首先应该想到|0了
		System.out.println(getIntegerToBinary(0));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(0 | 1644687));
		System.out.println("======================================>");
		//这个你会发现是往上填数据
		System.out.println(getIntegerToBinary(0));
		System.out.println(getIntegerToBinary(0xf));
		//低4位填1
		System.out.println(getIntegerToBinary(0 | 0xf));
		//高4位填1,这里没写0xf0000000,就是说一下这个数怎么来的
		System.out.println(getIntegerToBinary(0 | (0xf << 28)));
		//高低四位同时填1      0 | 0xf000000f是一样的
		System.out.println(getIntegerToBinary(0 | (0xf | (0xf << 28))));
		System.out.println("======================================>");
		
		// ^ 按位异或
		//两位不一样为1,一样为假
		System.out.println(0 ^ 0);
		System.out.println(0 ^ 1);
		System.out.println(1 ^ 0);
		System.out.println(1 ^ 1);
		System.out.println("======================================>");
		//有效位的头两位都是不一样的吧,所以最后结果是110
		System.out.println(getIntegerToBinary(3).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(3 ^ 5).substring(24));
		System.out.println("======================================>");
		//这个更有趣,有什么发现?好好想想
		System.out.println(getIntegerToBinary(7).substring(24));
		System.out.println(getIntegerToBinary(5).substring(24));
		System.out.println(getIntegerToBinary(7 ^ 5).substring(24));
		System.out.println("======================================>");
		//这是神马?还没发现么?传说中的取反
		System.out.println(getIntegerToBinary(-1));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(-1 ^ 1644687));
		//有什么感想?呵呵
		System.out.println((-1 ^ 1644687) + 1);
		System.out.println("======================================>");
		//0 ^ 上任何数,结果还是这个数,这让我们想起来什么了呢?
		//跟按位或一样的是吧。
		System.out.println(getIntegerToBinary(0));
		System.out.println(getIntegerToBinary(1644687));
		System.out.println(getIntegerToBinary(0 ^ 1644687));
		System.out.println("======================================>");
		//按位异或的魅力不止于此,它可以实现局部取反
		System.out.println(getIntegerToBinary(0x6ffffff6));
		//低4位取反
		System.out.println(getIntegerToBinary(0x6ffffff6 ^ 0xf));
		//高4位取反
		System.out.println(getIntegerToBinary(0x6ffffff6 ^ 0xf0000000));
		//高低四位同时取反
		System.out.println(getIntegerToBinary(0x6ffffff6 ^ 0xf000000f));
		System.out.println("======================================>");
		
		// ~ 取反运算符
		// 0变1 1变0
		//但跟 ^ 不一样,~是全取反,不能实现局部取反
		System.out.println(getIntegerToBinary(0));
		System.out.println(getIntegerToBinary(~0));
		System.out.println("======================================>");
		System.out.println(getIntegerToBinary(1));
		System.out.println(getIntegerToBinary(~1));
		System.out.println("======================================>");
	}
	
	public static final String DEFAULT_INT_ZERO = "00000000000000000000000000000000";
	public static final String getIntegerToBinary(int value)
	{
		String binary = Integer.toBinaryString(value);
		int length = Integer.SIZE - binary.length();
		return DEFAULT_INT_ZERO.substring(0, length) + binary;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值