package com.ghs.test; publicclass BinaryTest {publicstaticvoidmain(String[] args) {int i1 =10;int i2 = -10;int i3 =0;int i4 =2147483647;int i5 = -2147483648; System.out.println(Integer.toBinaryString(i1)); System.out.println(Integer.toBinaryString(i2)); System.out.println(Integer.toBinaryString(i3)); System.out.println(Integer.toBinaryString(i4)); System.out.println(Integer.toBinaryString(i5)); } }
输出结果:(总共是32位二进制,前面的0被省略了)
三、位运算
注:位运算符在整数范围内对位操作

1、逻辑运算符

~:按位非(NOT)(一元运算)&:按位与(AND)|:按位或(OR)^:按位异或(XOR)package com.ghs.test; publicclass TestBinary {publicstaticvoidmain(String[] args) {int a =2;//00000000000000000000000000000010int b =3;//00000000000000000000000000000011 System.out.println(Integer.toBinaryString(~a)+":"+~a); System.out.println(Integer.toBinaryString(a&b)+":"+(a&b)); System.out.println(Integer.toBinaryString(a|b)+":"+(a|b)); System.out.println(Integer.toBinaryString(a^b)+":"+(a^b)); } }
运行结果:
分析:2的十六进制表示:00000002;3的十六进制表示:00000003。~2的十六进制表示fffffffD(-3);a&b的十六进制表示00000002(2);a|b的十六进制表示00000003(3);a^b的十六进制表示00000001(1)。

2、移位运算符