java位操作和逻辑操作

[size=small][color=blue]1.java的位运算符[/color][/size]

/**
* 计算机对有符号数(包括浮点数)的表示有三种方法:原码、反码和补码, 补码=反码+1。 在 二进制里,是用 0 和 1
* 来表示正负的,最高位为符号位,最高位为 1 代表负数,最高位为 0 代表正数。
*
* @author Administrator
*
*/
public class ShiftOperator {

public static void main(String[] args) {
leftMove();
rightMove();
unSignRightMove();
}

/**
* 左移位运算符(<<)能将运算符左边的运算对象向左移动运算符右侧指定的位数(在低位补 0)
*
* 负数的情况:十进制如何转为二进制 1.先将-5的绝对值转换成二进制,即为0000 0101 2.然后求该二进制的反码,即为 1111 1010
* 3.最后将反码加1,即为:1111 1011
* Integer是32位(bit)的,所以结果是:11111111111111111111111111111011
*/
public static void leftMove() {
System.out.println("左移运算符");
int start_1 = 5;
System.out.println(start_1 + ":" + Integer.toBinaryString(start_1));
int end_1 = 5 << 2;
System.out.println(end_1 + ":" + Integer.toBinaryString(end_1));

int start_2 = -5;
System.out.println(start_2 + ":" + Integer.toBinaryString(start_2));
int end_2 = -5 << 2;
System.out.println(end_2 + ":" + Integer.toBinaryString(end_2));
}

/**
* "有符号"右移位运算符(>>)则将运算符左边的运算对象向右移动运算符右侧指定的位数 若值为正,则在高位插入0;若值为负,则在高位插入1
*/
public static void rightMove() {
System.out.println("(有符号)右移位运算符运算符");
int start_1 = 5;
System.out.println(start_1 + ":" + Integer.toBinaryString(start_1));
int end_1 = 5 >> 2;
System.out.println(end_1 + ":" + Integer.toBinaryString(end_1));

int start_2 = -5;
System.out.println(start_2 + ":" + Integer.toBinaryString(start_2));
int end_2 = -5 >> 2;
System.out.println(end_2 + ":" + Integer.toBinaryString(end_2));
}

/**
* 右移位运算符(>>>):零填充高位
*/
public static void unSignRightMove() {
System.out.println("右移位运算符运算符");
int start_1 = 5;
System.out.println(start_1 + ":" + Integer.toBinaryString(start_1));
int end_1 = 5 >>> 2;
System.out.println(end_1 + ":" + Integer.toBinaryString(end_1));

int start_2 = -5;
System.out.println(start_2 + ":" + Integer.toBinaryString(start_2));
int end_2 = -5 >>> 2;
System.out.println(end_2 + ":" + Integer.toBinaryString(end_2));
}
}


[size=small][color=blue]2.java逻辑操作符[/color][/size]

/**
* & 与;| 或;~ 非(也叫做求反);^ 异或
*
* @author Administrator
*
*/
public class LogicOperator {

public static final String INT_ZENO = "00000000000000000000000000000000";

public static void main(String[] args) {
operator_1();
operator_2();
operator_3();
operator_4();
}

/**
* 两个都为 1时才为1,否则为0
*/
public static void operator_1() {
System.out.println("与运算符(&)");
int first_1 = 5;
int second_1 = 3;
System.out.println(first_1 + ":"
+ getFullBinary(Integer.toBinaryString(first_1)));
System.out.println(second_1 + ":"
+ getFullBinary(Integer.toBinaryString(second_1)));
int result_1 = 5 & 3;
System.out.println(result_1 + ":"
+ getFullBinary(Integer.toBinaryString(result_1)));

int first_2 = -5;
int second_2 = 3;
System.out.println(first_2 + ":"
+ getFullBinary(Integer.toBinaryString(first_2)));
System.out.println(" " + second_2 + ":"
+ getFullBinary(Integer.toBinaryString(second_2)));
int result_2 = -5 & 3;
System.out.println(" " + result_2 + ":"
+ getFullBinary(Integer.toBinaryString(result_2)));
}

/**
* 只要有一个为1,结果为1
*/
public static void operator_2() {
System.out.println("或运算符(|)");
int first_1 = 5;
int second_1 = 3;
System.out.println(first_1 + ":"
+ getFullBinary(Integer.toBinaryString(first_1)));
System.out.println(second_1 + ":"
+ getFullBinary(Integer.toBinaryString(second_1)));
int result_1 = 5 | 3;
System.out.println(result_1 + ":"
+ getFullBinary(Integer.toBinaryString(result_1)));

int first_2 = -5;
int second_2 = 3;
System.out.println(first_2 + ":"
+ getFullBinary(Integer.toBinaryString(first_2)));
System.out.println(" " + second_2 + ":"
+ getFullBinary(Integer.toBinaryString(second_2)));
int result_2 = -5 | 3;
System.out.println(result_2 + ":"
+ getFullBinary(Integer.toBinaryString(result_2)));
}

/**
* 1变为0,0变为1
*/
public static void operator_3() {
System.out.println("非运算符(~)");
int first_1 = 5;
System.out.println(" " + first_1 + ":"
+ getFullBinary(Integer.toBinaryString(first_1)));
int result_1 = ~5;
System.out.println(result_1 + ":"
+ getFullBinary(Integer.toBinaryString(result_1)));
}

/**
* 两个相同时为1,不同时为0
*/
public static void operator_4() {
System.out.println("异或运算符(^)");
int first_1 = 5;
int second_1 = 3;
System.out.println(first_1 + ":"
+ getFullBinary(Integer.toBinaryString(first_1)));
System.out.println(second_1 + ":"
+ getFullBinary(Integer.toBinaryString(second_1)));
int result_1 = 5 ^ 3;
System.out.println(result_1 + ":"
+ getFullBinary(Integer.toBinaryString(result_1)));

int first_2 = -5;
int second_2 = 3;
System.out.println(first_2 + ":"
+ getFullBinary(Integer.toBinaryString(first_2)));
System.out.println(" " + second_2 + ":"
+ getFullBinary(Integer.toBinaryString(second_2)));
int result_2 = -5 ^ 3;
System.out.println(result_2 + ":"
+ getFullBinary(Integer.toBinaryString(result_2)));
}

private static String getFullBinary(String binaryString) {
return INT_ZENO.substring(binaryString.length()) + binaryString;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值