Java 位运算总结

在Java语言中,二进制使用补码表示,最高位是符号位,正数为0,负数为1。

其中:

  1. 正数的最高位是0,其余各位代表数值本身
  2. 负数的最高位是1,通过该数绝对值的补码按位取反,再对整个数加1

Java中的位移操作,只对int类型的数据生效(对byte,short,char,long的支持是转换成int后生效)。

一个int是4个字节,一个字节8位,总共32位。

在日常的开发中,位运算使用的不多,但是合理的使用,会使得代码大大简化。

比如在判断状态是0还是1时,在和0和1之间相互切换,可以直接使用原始值进行异或操作 1^n。

Java支持的位运算符主要包括:

  1. |或
  2. &且
  3. ~非
  4. ^异或
  5. <<左移
  6. >>右移
  7. >>>无符号右移

| 按位或

只要有1个为1,值就为1

	    	 // 7 的二进制: 0000 0111
	    	 // 9 的二进制: 0000 1001
	    	 
	    	 // |   有一个为1,值就为1   0000 1111
	    	 System.out.println(7|9);

& 按位且

同时为1,才是1

	    	 // 7 的二进制: 0000 0111
	    	 // 9 的二进制: 0000 1001
	    	 
	    	 // &   同时为1,才是1      0000 0001
	    	 System.out.println(7&9);

~ 按位非

1变为0,0变为1

	    	 // 7 的二进制:   0000 0000 0000 0000 0000 0000 0000 0111
	    	 // ~   0变1,1变0 1111 1111 1111 1111 1111 1111 1111 1000
	    	 System.out.println(~7);

^ 按位异或

相同为0,不同为1

	    	 // 7 的二进制: 0000 0111
	    	 // 9 的二进制: 0000 1001
	    	 
	    	 // ^   相同则为0,不同为1   0000 1110
	    	 System.out.println(7^9);

<< 按位左移

左移,右侧补0

	    	 // 7 的二进制: 0000 0111
	    	 // 9 的二进制: 0000 1001
	    	 
	    	 // <<  左移2位,右侧补0    0010 0100
	    	 System.out.println(9<<2);

>> 按位右移

右移,如果是正数,左侧补0;如果是负数,左侧补1

	    	 // 9 的二进制: 0000 1001
	    	 
	    	 // >>  右移2位,9为正数,高位全部补0    0000 0010  
	    	 System.out.println(9>>2);
	    	 
	         // -9 的二进制: 1111 1111 1111 1111 1111 1111 1111 0111
	    	 // >>  右移2位,-9为负数,高位全部补1    1111 1111 1111 1111 1111 1111 1111 1101
	    	 System.out.println(-9>>2);

>>> 无符号右移

和右移的区别在于,符号位也跟着右移。

	    	 // -1 表示 1111 1111 1111 1111 1111 1111 1111 1111  
	    	 System.out.println(-1>>1);
	    	 System.out.println(-1>>>1);
	    	 System.out.println(-1>>>2);
	    	 System.out.println(-1>>>3);

运行结果是:

-1
2147483647
1073741823
536870911

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java复数类的实现及运算实验总结: 1. 复数类的实现 为了表示复数,我们可以创建一个名为Complex的类。该类应该有两个实例变量:一个表示实部,另一个表示虚部。我们还需要实现一些方法来执行基本的数学运算,如加法、减法、乘法和除法。 下面是一个简单的Java复数类的实现: ```java public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public Complex add(Complex other) { double newReal = this.real + other.real; double newImaginary = this.imaginary + other.imaginary; return new Complex(newReal, newImaginary); } public Complex subtract(Complex other) { double newReal = this.real - other.real; double newImaginary = this.imaginary - other.imaginary; return new Complex(newReal, newImaginary); } public Complex multiply(Complex other) { double newReal = this.real * other.real - this.imaginary * other.imaginary; double newImaginary = this.real * other.imaginary + this.imaginary * other.real; return new Complex(newReal, newImaginary); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double newReal = (this.real * other.real + this.imaginary * other.imaginary) / denominator; double newImaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator; return new Complex(newReal, newImaginary); } @Override public String toString() { return String.format("%.2f + %.2fi", real, imaginary); } } ``` 2. 复数运算实验 我们可以编写一些简单的测试来验证我们的复数类是否运行良好。例如,我们可以创建两个复数并将它们相加: ```java public static void main(String[] args) { Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex result = c1.add(c2); System.out.println(result); } ``` 输出结果应该为“4.00 + 6.00i”。 我们还可以测试减法、乘法和除法: ```java public static void main(String[] args) { Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex addResult = c1.add(c2); Complex subtractResult = c1.subtract(c2); Complex multiplyResult = c1.multiply(c2); Complex divideResult = c1.divide(c2); System.out.println("Addition: " + addResult); System.out.println("Subtraction: " + subtractResult); System.out.println("Multiplication: " + multiplyResult); System.out.println("Division: " + divideResult); } ``` 输出结果应该为: ``` Addition: 4.00 + 6.00i Subtraction: -2.00 - 2.00i Multiplication: -5.00 + 10.00i Division: 0.44 + 0.08i ``` 3. 总结 我们已经成功地实现了一个Java复数类,并测试了它的基本数学运算。通过创建一个复数类,我们可以更轻松地进行复数计算,并且可以更容易地将复数与其他代码集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值