位运算符——与(&)、或(|)、异或(^)、非(~)、<<(左移运算符)和 >>(右移运算符)
package com.fh.base; public class WeiYunSuanFuTest { public static void main(String[] args) { int a = 10;//1010 int b = 8;//1000 System.out.println("a&b=" + (a&b) + "________" + Integer.toBinaryString((a&b)));//1000(对应位都是1为1,否则为0) System.out.println("a|b=" + (a|b) + "________" + Integer.toBinaryString((a|b)));//1010(对应位有一个是1就是1,否则都是0) System.out.println("a^b=" + (a^b) + "________" + Integer.toBinaryString((a^b)));//0010(对应位相同则为1,否则为0) System.out.println("~b=" + (~b) + "________" + Integer.toBinaryString((~b)));/// 0111(取反) ~b=-(b+1) System.out.println(8 << 3);//8 << 3 = 8*3^2=64 ; 9 << 2 = 9*2^2=36 System.out.println(8>>3);// 8>>3 = 8/2^3=1 ;16>>2 = 16/2^2=4 } }
<<(左移运算符)和 >>(右移运算符)
0 | 0000 0000 | 1*2^0 |
2 | 0000 0010 | 1*2^1 |
4 | 0000 0100 | 1*2^2 |
8 | 0000 1000 | 1*2^3 |
16 | 0001 0000 | 1*2^4 |
32 | 0010 0000 | 1*2^5 |
64 | 0100 0000 | 1*2^6 |
128 | 1000 0000 | 1*2^7 |
1的位移 |