/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author 风停心止
*/
public class bitDemo {
public static void main(String[] args) {
System.out.println(1<<3);//计算2*2*2
//在一个int变量的从右侧开始倒数第2位存储数据1和0
int bData = 1;
//倒数第二位存储1
bData = bData|(1<<(2-1));//将bData的倒数第二位存1,只需bData|10
System.out.println(bData);
bData = bData&(~(1<<(2-1)));//存储0
System.out.println(bData);
//利用疑惑进行简单的加密与解密
byte[] b = {1,2,3,4,5};
byte key = 123;
for(int i=0;i<b.length;i++){
b[i] = (byte)(b[i]^key);//加密
System.out.print(b[i]+" ");
}
System.out.println();
for(int i=0;i<b.length;i++){
b[i] = (byte)(b[i]^key);
System.out.print(b[i]+" ");
}
System.out.println();
//移位操作
System.out.println("-5>>3 = "+(-5>>3));
System.out.println("5>>3 = "+(5>>3));
System.out.println("-5<<3 = "+(-5<<3));
System.out.println("5<<3 = "+(5<<3));
//无符号移位操作
System.out.println("5>>>3 = "+(5>>>3));
System.out.println("-5>>>3 = "+(-5>>>3));
}
}