Implement Multi-byte shift in Java

Source: Internet
Author: User
//// The algorithm is very simple, that is, each byte is moved, the bit of the shift overflow is retained and saved as a byte for combination with the next byte. Public static void main (string [] ARGs) {byte [] DATA = hex. fromstring ("00e8080000000065289bbaef2ec78ccd0f13571ad01bae68000001bbb00818"); byte [] out = rightshift (data, (byte) 3); system. out. println (hex. tostring (out);} public static byte [] rightshift (byte [] data, byte shiftval) {// 00e80800-> 0000 0000 1110 1000 0000 1000 | left shift-5 -----> 0001 1101 00 byte [] out = new byte [data. length + 1]; int offset = 0; byte first = (byte) (data [0] & 0xff)> shiftval )); out [offset ++] = first; For (INT I = 0; I <data. length-1; I ++) {// right shift 3 // get last 3 bits of this byte. combine these 3 bit as a byte. // that byte or the next shifted [3] Byte // --------------- // overflowed 3 bitbyte overflowed = (byte) (data [I] & 0xff) <(8-shiftval); byte next = (byte) (overflowed | (data [I + 1] & 0xff)> shiftval )); out [offset ++] = next;} return out ;}

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.