日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java byte 循环左移 循环右移 rotateLeft rotateRight

發布時間:2025/7/25 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java byte 循环左移 循环右移 rotateLeft rotateRight 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java byte 循環左移 循環右移 rotateLeft rotateRight

1.概念。

  循環左移:

    eg1:byte in = (byte) 0x01;【0000 0001】則循環左移2位后變為【0000 0100】

    eg2: btye in = (byte)0x90;[1001 0000],則循環左移2位變為[0100 0010]

  循環右移:

    eg3:byte in = (byte)0x01;[0000 0001]則循環右移2位后變為[0100 0000]

    eg4:byte in = (byte)0x90;[1001 0000]則循環右移2位后變為[0010 0100]

2.示例。

1 public class DataConvertUtil { 2 /** 3 * 循環左移 4 * @param sourceByte 待左移動的值 5 * @param n 左移動的為數 6 * @return 7 */ 8 public static byte rotateLeft(byte sourceByte, int n) { 9 // 去除高位的1 10 int temp = sourceByte & 0xFF; 11 return (byte) ((temp << n) | (temp >>> (8 - n))); 12 } 13 /** 14 * 循環右移 15 * @param sourceByte 16 * @param n 17 * @return 18 */ 19 public static byte rotateRight(byte sourceByte, int n) { 20 // 去除高位的1 21 int temp = sourceByte & 0xFF; 22 return (byte) ((temp >>> n) | (temp << (8 - n))); 23 } 24 /** 25 * 循環左移 26 * @param sourceBytes 27 * @param n 28 * @return 29 */ 30 public static byte[] rotateLeft(byte[] sourceBytes, int n) { 31 byte[] out = new byte[sourceBytes.length]; 32 for (int i = 0; i < sourceBytes.length; i++) { 33 out[i] = rotateLeft(sourceBytes[i], n); 34 } 35 return out; 36 } 37 38 public static byte[] rotateRight(byte[] sourceBytes, int n) { 39 byte[] out = new byte[sourceBytes.length]; 40 for (int i = 0; i < sourceBytes.length; i++) { 41 out[i] = rotateRight(sourceBytes[i], n); 42 } 43 return out; 44 } 45 }

3.一個簡單的測試類

1 import static org.junit.Assert.*; 2 3 import org.junit.Test; 4 5 public class DataConvertUtilTest { 6 7 @Test 8 public void test() { 9 System.out.println("originByte" 10 + "|" + "BinaryOrig" 11 + "|" + "outByte" 12 + "|" + "BinaryOut" 13 + "|" + "outLength"); 14 byte[] sourceBytes = new byte[]{(byte)(0x01),(byte)(0x03),5,(byte)(0x07),(byte)(0x0f) 15 ,(byte)(0x1f),(byte)(0x3F),(byte)(0x7F),(byte)(0xFF),(byte)(0xF0),(byte)(0xC0) 16 }; 17 byte[] out = DataConvertUtil.rotateRight(sourceBytes, 2); 18 for (int i = 0; i < out.length; i++) { 19 System.out.println("rright" 20 + "|" + sourceBytes[i] 21 + "|" + Integer.toBinaryString(sourceBytes[i]) 22 + "|" + Byte.toString(out[i]) 23 + "|" + Integer.toBinaryString(out[i]) 24 + "|" + Integer.toBinaryString(out[i]).length() 25 + "|" + Integer.toBinaryString(out[i]).indexOf('0')); 26 } 27 System.out.println(); 28 out = DataConvertUtil.rotateLeft(sourceBytes, 2); 29 for (int i = 0; i < out.length; i++) { 30 System.out.println("rleft" 31 + "|" + sourceBytes[i] 32 + "|" + Integer.toBinaryString(sourceBytes[i]) 33 + "|" + Byte.toString(out[i]) 34 + "|" + Integer.toBinaryString(out[i]) 35 + "|" + Integer.toBinaryString(out[i]).length() 36 + "|" + Integer.toBinaryString(out[i]).indexOf('0')); 37 } 38 } 39 40 }

?

轉載于:https://www.cnblogs.com/cobble19/archive/2013/01/29/2881283.html

總結

以上是生活随笔為你收集整理的java byte 循环左移 循环右移 rotateLeft rotateRight的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。