欢迎访问 生活随笔!

生活随笔

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

编程问答

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

发布时间:2025/7/25 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。