Arithmetic left shift operator
In binary form, all numbers are shifted to the left by the corresponding number of digits, the high position is removed (discarded), and the low vacancy is 0.
syntax format:
Number of shifts required for numbers << shifts
For example: 3 << 2, the number 3 is shifted to the left 2-bit
Package com.zzwx.test.shifting;public class main {public static void main ( String[] args) {for (int p = 1; p < 4; p++) { System.out.println ("////////////arithmetic left shift - " + p + "/////////////");for (int n = 1; n <= 4; n++) {system.out.println ("Digital : with shift required" + p + \t convert binary : + tobinarystring (p) + " \t Shift Amount : " + n+ " \t shifted binary : "+ shifttobinaryleft ( Tobinarystring (P), n) + " \t value : " + (P << n));} System.out.println ("////////////arithmetic left shift - " + p + "/////////////");} System.out.println ("Arithmetic left shift ------> p << n = p * 2 "); System.out.println ("\n\n\n");} /** * Gets the binary * * @param of the current number num * int Digital * @return binary characters */public static String Tobinarystring (int num) {return integer.tobinarystring (num);} /** * get shifted binary * * @param by shift amount binaryStr * binary * @param required for shift shift * offset * @return shifted binary */public static string shifttobinaryleft (String binarystr, int shift) {for ( int i = 0; i < shift; i++) {binaryStr += "0";} Return binarystr;}}
Test results
Arithmetic left shift - 1/////////////need to shift the number : 1 convert binary : 1 Shift amount : 1 shifted binary : 10 value : 2 need to shift the numbers : 1 Convert binary : 1 shift amount : 2 shifted binary : 100 value : 4 need to shift the number : 1 convert binary : 1 shift amount : 3 Post-shift binary : 1000 value : 8 need to shift the numbers : 1 convert the binary : 1 shift Volume : 4 shifted binary : 10000 value : 16//////////// Arithmetic left shift - 1/////////////////////////arithmetic left shift - 2/////////////need to shift the numbers : 2 Conversion of binary : 10 shift volume : 1 shifted binary : 100 value : 4 need to shift the number : 2 convert binary : 10 shift amount : 2 post-shift binary : 1000 value : 8 need to shift the number : 2 convert binary : 10 shift amount : 3 Post-shift binary : 10000 value : 16 need to shift the number : 2 convert binary : 10 shift Volume : 4 shifted binary : 100000 value : 32/// Arithmetic left shift - 2/////////////////////////arithmetic left shift - 3/////////////need to shift the number : 3 convert binary : 11 shift Volume : 1 post-shift binary : 110 Value : 6 need to shift the number : 3 convert binary : 11 shift amount : 2 Post-shift binary : 1100 value : 12 need to shift the number : 3 convert binary : 11 shift Volume : 3 shifted binary : 11000 value : 24 need to shift the number : 3 convert binary : 11 shift amount : 4 shifted binary : 110000 value : 48////////////arithmetic left shift - 3/////////////arithmetic left shift ------> p << n = p * 2 N-Th square
Arithmetic left shift of Java shift operation