1 public class jinzhi { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 6 Integer[] a = getIndexFromSncTpId(0x01011213,0); 7 for (int i = 0; i < a.length; i++) { 8 System.out.println(a[i]); 9 } 10 System.out.println("--------------------------------"); 11 int b = 0x01011213 ; 12 System.out.println(b); 13 System.out.println(b >> 24); 14 System.out.println(b >> 16); 15 System.out.println(b >> 8); 16 System.out.println(b >> 0); 17 System.out.println(b); 18 System.out.println("--------------------------------"); 19 System.out.println("----------------反解------------------"); 20 21 //Math.pow(x,y)的作用就是计算x的y次方,其计算后是浮点数 22 int tpIdH = (int)(1 * Math.pow(16, 6) + 1 * Math.pow(16, 4) + 18 * Math.pow(16, 2) + 19); 23 System.out.println(tpIdH); 24 int tpIdH1 = (int)(1 * Math.pow(2, 24) + 1 * Math.pow(2, 16) + 18 * Math.pow(2, 8) + 19); 25 System.out.println(tpIdH1); 26 27 } 28 29 public static Integer[] getIndexFromSncTpId(int tpIdH, int tpIdL){ 30 Integer index[] = new Integer[8]; 31 32 for(int i = 0; i < 4; i ++) 33 index[i] = ((int) tpIdH >> 8*(3 - i)) & 0xff; 34 35 for(int i = 4; i < 8; i ++) 36 index[i] = ((int) tpIdL >> 8*(7 - i)) & 0xff; 37 38 39 return index; 40 }
结果:
1 1 18 19 0 0 0 0 -------------------------------- 16847379 1 257 65810 16847379 16847379 -------------------------------- ----------------反解------------------ 16847379 16847379 16847379
解析:
0x01011213 二进制为 1000000010001001000010011 共25位,补足32位为:00000001000000010001001000010011
网元分八位为一组,共4组 :
slot subSlot port subport
00000001 | 00000001 | 00010010 | 00010011
1 1 18 19
右移24位就是获取 slot ; 右移16位就是获取 subslot ; 右移8位就是获取 port ;右移0位就是获取 subport
&0xff 就是去掉左边多余的值 0xff 二进制为 11111111 如果遇到多余8位的,前面补0 如16位的 00010010 | 00010011 & 0xff 等同于
00010010 | 00010011
&
00000000 | 11111111
00000000 | 00010011 = 19
如获取port 二进制为 00010010 由 00000001 | 00000001 | 00010010 | 00010011 向右位移8位为 00000001 | 00000001 | 00010010 想要获取port所代表的的值就要&0xff(11111111)去除port左边16位的值
00000001 | 00000001 | 00010010
&
00000000 | 00000000 | 11111111
00000000 | 00000000 | 00010010 = 18