java 位操作 bitwise(按位) operation bit

操作longValue = longValue | (1 << n); 可以在longValue的2进制表示中,把[color=red]从右边数到左边数第n + 1位的值设置为1[/color],并且不影响其他位上面的值 即用0和2进制值变量x做或|or操作,不会影响到2进制变量x的值

操作longValue = longValue & ~(1 << n); 可以在longValue的2进制表示中,把[color=red]从右边数到左边数第n + 1位设置为0[/color],并且不影响其他位上面的值 即用1和2进制值变量x做与&and操作,不会影响到2进制变量x的值

操作System.out.println((longValue >> n & 1) == 1); 可以判断值longValue的2进制表示中,[color=red]从右边数到左边第n + 1位的值是0false 还是1true[/color]


public class bitOperation {

/**
* @param args
*/
public static void main(String[] args) {

long longValue = 0;

longValue = longValue | (1 << 0);
// 1
System.out.println(Long.toBinaryString(longValue));
longValue = longValue | (1 << 1);
// 11
System.out.println(Long.toBinaryString(longValue));
longValue = longValue | (1 << 4);
// 10011
System.out.println(Long.toBinaryString(longValue));
longValue = longValue | (1 << 5);
// 110011
System.out.println(Long.toBinaryString(longValue));
longValue = longValue | (1 << 6);
// 1110011
System.out.println(Long.toBinaryString(longValue));

String hex = Long.toBinaryString(longValue);
// 1110011
System.out.println(hex);
// 115
System.out.println(Integer.valueOf("1110011", 2));
// 1110011
System.out.println(Long.toBinaryString(longValue >> 0));
// 1
System.out.println(Long.toBinaryString(longValue >> 0 & 1));
// 111001
System.out.println(Long.toBinaryString(longValue >> 1));
// 1
System.out.println(Long.toBinaryString(longValue >> 1 & 1));
// true
System.out.println((longValue >> 0 & 1) == 1);
// true
System.out.println((longValue >> 1 & 1) == 1);
// false
System.out.println((longValue >> 2 & 1) == 1);
// false
System.out.println((longValue >> 3 & 1) == 1);
// true
System.out.println((longValue >> 4 & 1) == 1);
// true
System.out.println((longValue >> 5 & 1) == 1);
// true
System.out.println((longValue >> 6 & 1) == 1);
// false
System.out.println((longValue >> 7 & 1) == 1);

// Demonstrate the bitwise logical operators.
bitLogic();
// Left shifting a byte value.
byteShift();
}

/**
* Left shifting a byte value.
*/
private static void byteShift() {
byte a = 64, b;
int i;

i = a << 2;
b = (byte) (a << 2);

// Original value of a: 64
System.out.println("Original value of a: " + a);
// i and b: 256 0
System.out.println("i and b: " + i + " " + b);
System.out.println("\r\n");
}

/**
* Demonstrate the bitwise logical operators.
*/
private static void bitLogic() {
String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
"1110", "1111"

};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;

// a = 0011 = 3
System.out.println(" a = " + binary[a] + " = " + a);
// b = 0110 = 6
System.out.println(" b = " + binary[b] + " = " + b);
// a|b = 0111 = 7
System.out.println(" a|b = " + binary[c] + " = " + c);
// a&b = 0010 = 2
System.out.println(" a&b = " + binary[d] + " = " + d);
// a^b = 0101 = 5
System.out.println(" a^b = " + binary[e] + " = " + e);
// ~a&b|a&~b = 0101 = 5
System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);
// ~a = 1100 = 12
System.out.println(" ~a = " + binary[g] + " = " + g);
System.out.println("\r\n");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值