Arithmetic left shift operator
>> operation rules:according to the binary form to move all the numbers to the right of the towering digits, low-shifted out (discard), high position of the vacancy complement the sign, that is, positive 0, negative complement 1.
Syntax Format:
Number of digits >> shifts that need to be shifted
package com.zzwx.test.shifting;public class mainright {public static void Main (String[] args) {for (int p = 50; p < 55; p++) { System.out.println ("////////////Arithmetic right 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 : "+ shifttobinaryright ( Tobinarystring (P), n) + " \t value : " + (P >> n));} System.out.println ("////////////Arithmetic right shift - " + p + "/////////////");} System.out.println ("Arithmetic right shift ------> p >> n = [p / 2 N-Th square >= 1 --> , Rounding] | | [p / 2 N-Th square < 1 --> 0] "); /** * getting the binary * * @param of the current number num * int Digital * @return binary character */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 shifttobinaryright (String binarystr, int shift) {binaryStr = binarystr.substring (0, binarystr.length () - shift) + "." +binarystr.substring (Binarystr.length () - shift, binarystr.length ()); return binarystr;}}
Test results
Arithmetic right shift - 50/////////////need to shift the numbers : 50 convert binary : 110010 Shift Volume : 1 shifted binary : 11001.0 value : 25 need to shift the digital : 50 convert binary : 110010 shift Volume : 2 post-shift binary : 1100.10 value : 12 need to shift the numbers : 50 convert binary : 110010 Shift amount : 3 shifted binary : 110.010 value : 6 need to shift the number &NBSP;:&NBSP;50 convert binary : 110010 shift Volume : 4 post-shift binary : 11.0010 value : 3////////////arithmetic right shift - 50/////////////////////////arithmetic right shift - 51//////// Need to shift the number : 51 convert binary : 110011 shift amount : 1 The shifted binary : 11001.1 value : 25 needs to shift the number : 51 convert the binary : 110011 Shift Volume : 2 shifted binary : 1100.11 value : 12 need to shift the numbers : 51 Convert binary : 110011 shift Volume : 3 post-shift binary : 110.011 Value : 6 need to shift the number : 51 convert binary : 110011 shift amount : 4 shifted binary : 11.0011 value : 3////////////arithmetic right shift - 51//////// Arithmetic right shift - 52/////////////need to shift the number : 52 convert binary : 110100 shift Volume : 1 shifted binary : 11010.0 value : 26 numbers that need to be shifted : 52 convert binary : 110100 shift amount : 2 shifted binary : 1101.00 value : 13 need to shift the numbers : 52 convert binary : 110100 Shift Volume : 3 shifted binary : 110.100 value : 6 need to shift the digital : 52 Convert binary &NBsp;: 110100 shift Volume : 4 shifted binary : 11.0100 value : 3////////////arithmetic right shift - 52/////////////////////////arithmetic right shift - 53/////////////need to shift the numbers : 53 conversion of Binary : 110101 shift volume : 1 shifted binary : 11010.1 value : 26 need to shift the numbers : 53 convert binary : 110101 Shift amount : 2 shifted binary : 1101.01 value : 13 need to shift the number : 53 convert binary : 110101 shift Volume : 3 post-shift binary : 110.101 value : 6 need to shift the number : 53 convert binary : 110101 shift amount : 4 shifted binary : 11.0101 value : 3////////////arithmetic right shift - 53/ Arithmetic right shift - 54/////////////need to shift the numbers : 54 convert binary : 110110 Shift Volume &Nbsp;: 1 shifted binary : 11011.0 value : 27 need to shift the number : 54 convert binary : 110110 shift Volume : 2 post-shift binary : 1101.10 value : 13 need to shift the number : 54 convert binary : 110110 shift amount : 3 shifted binary : 110.110 value : 6 need to shift the numbers : 54 Convert binary : 110110 shift Volume : 4 post-shift binary : 11.0110 value : 3////////////arithmetic right shift - 54/////////////arithmetic right shift ------>&NBSP;&NBSP;&NBSP;&NBSP;P >> n = [p / 2 N-Order >= 1 --> rounding] | | [p / 2 N-Th square < 1 --> 0]
Arithmetic right shift of Java shift operation