Java Byte shift Operation considerations

Source: Internet
Author: User
Tags bitwise

Java to byte +-*/>> >>> << & | ^ (add, subtract, multiply, divide, shift right, shift left, unsigned right, bit and, bit or, bitwise XOR) operations, will be the first to convert a byte to int, then the operation. This fact can lead to a variety of problems:

Suppose we want to do the following byte operation: 1111 1000 to the right 1 bits, then 0000 0001 or operation, 0111 1101. The intuitive writing procedure is as follows:byte B = 0xf8;byte B2 = b >> 1 | 0x01; There are multiple errors in this notation, which are now corrected individually: 1 compiler error, int cannot be converted directly to bytein order to solve this problem, strengthen the system transformation. byte B = (byte) 0xf8;byte b2 = (byte) ((b >> 1) | 0x01); 2 output is 1111 1101 not what we want. 0011 1101The reason is that >> is signed to the right, and when the sign bit is 1 o'clock, the left side is 1 instead of 0. Modify to use >>> unsigned right shift:byte B = (byte) 0xf8;byte b2 = (byte) ((b >>> 1) | 0x01);3 The output remains 1111 1101 after operationThe reason is that byte is converted to int before the operation, so the decomposition of the operation steps are as follows:Convert b to int 1111 1000 to 11111111 11111111 11111111 11111000unsigned Right shift 1-bit 01111111 11111111 11111111 11111100with 0x01 bitwise OR 01111111 11111111 11111111 11111101force conversion back to byte 11111101solution, precede the right-shift operation with 0xFFbyte B = (byte) 0xf8;byte b2 = (Byte) (((b & 0xff) >>> 1) | 0x01); Note that parentheses must be added because >>> has a higher priority than &4 after running the discovery output for the result we want 0111 1101. The operation steps are broken down as follows:Convert b to int 1111 1000 to 11111111 11111111 11111111 11111000& operation with 0xFF 00000000 00000000 00000000 11111000unsigned Right shift 1-bit 00000000 00000000 00000000 01111100with 0x01 bitwise OR 00000000 00000000 00000000 01111101force conversion back to byte 011111015 about System.out.println ();byte B = (byte) 0xf8;System.out.println (b); --The final output is-8The operation steps are:Convert b to int 1111 1000 to 11111111 11111111 11111111 11111000take sign bit- -1111111 11111111 11111111 11111000Take back +1 (because it is in complement) -0000000 00000000 00000000 00001000Output-8 Final Conclusion: 1 Distinguishing between >> and >>> 2 First & 0xFF before >> operation 3 Note the symbol priority, using parentheses correctly. 4 It is important to note that & 's priority is less than +. So the result of a = B & 0xFF + 2000 may not be what you want .Attached: a function to print byte,int each bit value. public static void Printbyte (Byte b) {for (int i = 7; I >=0; I-) {int shiftleft = (b >> i) & 0x01        ;    System.out.print (Shiftleft); } System.out.println ();}         public static void Printint (Int. b) {for (int i = +; I >=0; I-) {int shiftleft = (b >> i) & 0x01;    System.out.print (Shiftleft); } System.out.println ();}

Java Byte shift Operation considerations

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.