Java operator displacement

Source: Internet
Author: User
Package util; public class bitoperator {public static void main (string [] ARGs) {system. out. println ("\ n shifts left by bit"); output ("123", 123); output ("123 <32", 123 <32); system. out. println ("\ n shifts right by bit"); output ("123", 123); output ("123> 2", 123> 2); system. out. println ("\ n shifts to the right by bit, while shifting to the right when the high bit supplements the sign bit"); output ("-123",-123); output ("-123> 2 ", -123> 2); system. out. println ("\ n shifts right by bit"); output ("123", 123); output ("123 >>> 1", 123 >>> 1 ); system. out. println ("\ n shifts right by bit unsigned, 0 is added for the high position when the right shift"); output ("-123",-123 ); output ("-123 >>> 2",-123 >>>> 2);} Private Static void output (string prompt, int result) {system. out. printf ("% 10 s = %-10D % S % N", prompt, result, Tobit (result);} public static string Tobit (INT num) {char [] CHS = new char [39]; for (INT I = 0, K = 0; I <integer. size; I ++) {CHS [k ++] = (char) ('0' + (Num >>> integer. size-I-1) & 1); If (I & 3) = 3 & K <chs. length) {CHS [k ++] = '';} return new string (CHS );}} /* shift left by bit 123 = 123 0000 0000 0000 0000 0000 0000 0111 1011 123 <2 = 492 0000 0000 0000 0000 0000 0001 shifted right by bit with a symbol 1110 = 1100 123 123 0000 0000 0000 0000 0000 0111 1011> 2 = 30 123 0000 0000 0000 0000 0000 0000 shifted right by bit and symbol, right Shift time high bit supplement sign bit-123 =-123 1111 1111 1111 1111 1111 1111 1000-0101> 2 =-31 123 1111 1111 1111 1111 1111 shifted right by bit unsigned 1111 = 123 0000 0000 0000 0000 0000 0000 0111 1011 123> 2 = 30 0000 0000 0000 0000 0000 0000 shift right by bit unsigned, right Shift time high position fill 0-123 =-123 1111 1111 1111 1111 1111 1111 1000-0101> 2 = 123 1073741793 0011 1111 1111 1111 1111 1111 1110 */

Related Article

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.