Arithmetic left shift of Java shift operation

Source: Internet
Author: User
Tags arithmetic

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.