=============print Bit==============
00000000 00000000 00000000 00000001 1
00000000 00000000 00000000 00000000 0
11111111 11111111 11111111 11111111 -1
=============test UNSIGNED RIGHT SHIFT==============
0>>>1=0
00000000 00000000 00000000 00000000
无符号右移1位:
00000000 00000000 00000000 00000000
1>>>1=0
00000000 00000000 00000000 00000001
无符号右移1位:
00000000 00000000 00000000 00000000
-1>>>1=2147483647
11111111 11111111 11111111 11111111
无符号右移1位:
01111111 11111111 11111111 11111111
1>>>2=0
00000000 00000000 00000000 00000001
无符号右移2位:
00000000 00000000 00000000 00000000
1>>>3=0
00000000 00000000 00000000 00000001
无符号右移3位:
00000000 00000000 00000000 00000000
-1>>>2=1073741823
11111111 11111111 11111111 11111111
无符号右移2位:
00111111 11111111 11111111 11111111
-1>>>3=536870911
11111111 11111111 11111111 11111111
无符号右移3位:
00011111 11111111 11111111 11111111
-3000>>>3=536870537
11111111 11111111 11110100 01001000
无符号右移3位:
00011111 11111111 11111110 10001001
---无符号右移操作左边以0补齐
=============test AND==============
1&1=1
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
1&0=0
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
0&1=0
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000000
0&0=0
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
=============test OR==============
1|1=1
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
1|0=1
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
0|1=1
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
0|0=0
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
=============test XOR==============
1^1=0
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000000
1^0=1
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
0^1=1
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000001
0^0=0
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
=============test UNSIGNED RIGHT SHIFT==============
0<<1=0
00000000 00000000 00000000 00000000
左移1位:
00000000 00000000 00000000 00000000
1<<1=2
00000000 00000000 00000000 00000001
左移1位:
00000000 00000000 00000000 00000010
-1<<1=-2
11111111 11111111 11111111 11111111
左移1位:
11111111 11111111 11111111 11111110
250609664<<3=2004877312
00001110 11110000 00000000 00000000
左移3位:
01110111 10000000 00000000 00000000
250609664<<5=-570425344
00001110 11110000 00000000 00000000
左移5位:
11011110 00000000 00000000 00000000
-250609664<<3=-2004877312
11110001 00010000 00000000 00000000
左移3位:
10001000 10000000 00000000 00000000
-250609664<<5=570425344
11110001 00010000 00000000 00000000
左移5位:
00100010 00000000 00000000 00000000
---左移操作右边以0补齐
=============test NOT==============
~0=-1
00000000 00000000 00000000 00000000
11111111 11111111 11111111 11111111
~1=-2
00000000 00000000 00000000 00000001
11111111 11111111 11111111 11111110
~-1=0
11111111 11111111 11111111 11111111
00000000 00000000 00000000 00000000
=============test RIGHT SHIFT==============
0>>1=0
00000000 00000000 00000000 00000000
右移1位:
00000000 00000000 00000000 00000000
1>>1=0
00000000 00000000 00000000 00000001
右移1位:
00000000 00000000 00000000 00000000
-1>>1=-1
11111111 11111111 11111111 11111111
右移1位:
11111111 11111111 11111111 11111111
1>>2=0
00000000 00000000 00000000 00000001
右移2位:
00000000 00000000 00000000 00000000
1>>3=0
00000000 00000000 00000000 00000001
右移3位:
00000000 00000000 00000000 00000000
-1>>2=-1
11111111 11111111 11111111 11111111
右移2位:
11111111 11111111 11111111 11111111
-1>>3=-1
11111111 11111111 11111111 11111111
右移3位:
11111111 11111111 11111111 11111111
-3000>>3=-375
11111111 11111111 11110100 01001000
右移3位:
11111111 11111111 11111110 10001001
---右移操作左边以符号位补齐
package com.test;
import org.junit.Test;
public class Main {
@Test
public void printBit(){
BT.p("=============print Bit==============");
BT.p(BT.b(1) + " " + 1);
BT.p(BT.b(0) + " " + 0);
BT.p(BT.b(-1) + " " + (-1));
}
@Test
public void or(){
BT.p("/n/n=============test OR==============");
BT.or(1, 1);
BT.or(1, 0);
BT.or(0, 1);
BT.or(0, 0);
}
@Test
public void and(){
BT.p("/n/n=============test AND==============");
BT.and(1, 1);
BT.and(1, 0);
BT.and(0, 1);
BT.and(0, 0);
}
@Test
public void xor(){
BT.p("/n/n=============test XOR==============");
BT.xor(1, 1);
BT.xor(1, 0);
BT.xor(0, 1);
BT.xor(0, 0);
}
@Test
public void not(){
BT.p("/n/n=============test NOT==============");
BT.not(0);
BT.not(1);
BT.not(-1);
}
@Test
public void rightShift(){
BT.p("/n/n=============test RIGHT SHIFT==============");
BT.rightShift(0,1);
BT.rightShift(1,1);
BT.rightShift(-1,1);
BT.rightShift(1,2);
BT.rightShift(1,3);
BT.rightShift(-1,2);
BT.rightShift(-1,3);
BT.rightShift(-3000,3);
BT.p("---右移操作左边以符号位补齐");
}
@Test
public void unsignedRightShift(){
BT.p("/n/n=============test UNSIGNED RIGHT SHIFT==============");
BT.unsignedRightShift(0,1);
BT.unsignedRightShift(1,1);
BT.unsignedRightShift(-1,1);
BT.unsignedRightShift(1,2);
BT.unsignedRightShift(1,3);
BT.unsignedRightShift(-1,2);
BT.unsignedRightShift(-1,3);
BT.unsignedRightShift(-3000,3);
BT.p("---无符号右移操作左边以0补齐");
}
@Test
public void leftShift(){
BT.p("/n/n=============test UNSIGNED RIGHT SHIFT==============");
BT.leftShift(0,1);
BT.leftShift(1,1);
BT.leftShift(-1,1);
BT.leftShift(0xef00000,3);
BT.leftShift(0xef00000,5);
BT.leftShift(-0xef00000,3);
BT.leftShift(-0xef00000,5);
BT.p("---左移操作右边以0补齐(符号位无特殊运算,当作普通位作移动)");
}
}
package com.test;
public class BT { //BitwiseOperationForTest
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String b(int i){
return bb(i,1);
}
public static String bb(int i,int shift){
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do{
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while(charPos>0); //(i != 0);
return
new String(buf, 0,8) + " " +
new String(buf, 8,8) + " " +
new String(buf, 16,8) + " " +
new String(buf, 24,8);
}
public static void p(String s){
System.out.println(s);
}
public static int or(int v1,int v2){
int result = v1 | v2;
p("" + v1 + "|" + v2 + "=" + result);
p(" " + b(v1));
p(" " + b(v2));
p(" " + b(result));
return result;
}
public static int and(int v1,int v2){
int result = v1 & v2;
p("" + v1 + "&" + v2 + "=" + result);
p(" " + b(v1));
p(" " + b(v2));
p(" " + b(result));
return result;
}
public static int xor(int v1,int v2){
int result = v1 ^ v2;
p("" + v1 + "^" + v2 + "=" + result);
p(" " + b(v1));
p(" " + b(v2));
p(" " + b(result));
return result;
}
public static int not(int v){
int result = ~v;
p("~" + v + "=" + result);
p(" " + b(v));
p(" " + b(result));
return result;
}
public static int rightShift(int v,int n){
int result = v >> n;
p("" + v + ">>" + n + "=" + result);
p(" " + b(v));
p(" 右移" + n + "位:");
p(" " + b(result));
return result;
}
public static int unsignedRightShift(int v,int n){
int result = v >>> n;
p("" + v + ">>>" + n + "=" + result);
p(" " + b(v));
p(" 无符号右移" + n + "位:");
p(" " + b(result));
return result;
}
public static int leftShift(int v,int n){
int result = v << n;
p("" + v + "<<" + n + "=" + result);
p(" " + b(v));
p(" 左移" + n + "位:");
p(" " + b(result));
return result;
}
}