Example of java's method for implementing binary processing based on shift operations


An example of java based on shift operation is presented in this paper. To share for your reference, specific as follows:

/**
 * @author openks
 * @since 2013-9-21  Shift operation instance
 */
public class TestDisplacement {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // 10 Hexadecimal Numbers 2 Moving to the left 3 position   namely  2 Into the system 10 Moving to the left 3 Who that is 10000  convert 10 Into the system for 2 the 4 To the power   namely 16
    System.out.println("2 Moving to the left 3 A: " + (2 << 3));
    System.out.println("7 Moving to the left 1 A: " + (7 << 1));
    System.out.println("7 Move to the right 1 A: " + (7 >> 1));
    int n = 3;
    System.out.println("2 the " + n + " The power: " + (int) Math.pow(2, n));
    System.out.println("1 Moving to the left " + n + " A: " + (1 << n));
    System.out.println(" visible 2 the N The power and 1 Shift to the left N The values of the bits are equal. ");
  }
}
/**
 * @author openks
 * @since 2013-9-21
 * 2 Into the system 10 Base processing   Can be used for permission control   Maximum management 32 A permissions
 */
public class TestBinary {
  /**
   *  To obtain 10 Hexadecimal Numbers k convert 2 Into the system after the first index The value of a
   * @param k 10 Hexadecimal Numbers
   * @param index  The first index position   (from 1 Start)
   * @return 10 Convert base number to 2 Into the system after the first index The value of a
   */
  public static int getValue(Integer k,int index){
    String string = Integer.toBinaryString(k);
    int len = string.length();
    System.out.println("2 The base string is: "+string+"\n A total of "+len+" position ");
    if(index>len){
      return 0;
    }else{
      return string.charAt(len-index)-'0';
    }
  }
  /**
   *  Set up the 10 Hexadecimal Numbers k convert 2 Into the system after the first index The value of the bit and returns the processed 10 Hexadecimal Numbers
   * @param k 10 Hexadecimal Numbers k
   * @param index  The first index position   (from 1 Start)
   * @param m  the index The value of the bit   only 0,1 Two options
   * @return  After processing the 10 Hexadecimal Numbers
   */
  public static int setValue(Integer k,int index,Integer m){
    // The equivalent of 2 the index-1 To the power
    Integer t = 1<<(index-1);
    if(t>k){
      if(m==1){
        return t+k;
      }else{
        return k;
      }
    }else{
      int m1 = getValue(k,index);
      if(m1==0){
        return k+t;
      }else{
        return k-t;
      }
    }
  }
  /**
   * @param args
   */
  public static void main(String[] args) {
    int a=25;// Original permission value
    int i=2;// The number of digits to view
    int d = 2;// The number of digits to modify
    a= setValue(a, d, 1);// Modify the first d A value of 1
    System.out.println(" The first "+i+" The bit value is: "+getValue(a,i));
  }
}

PS: Here are a few more computing and conversion tools for your reference:

Online any base conversion tool: http://tools.ofstack.com/transcoding/hexconvert

Scientific Calculators online Using _ Advanced calculators online calculation: http://tools.ofstack.com/jisuanqi/jsqkexue

Online calculators _ Standard calculators: http://tools.ofstack.com/jisuanqi/jsq

I hope this article is helpful for java programming.