zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    设备管理器里“SM总线控制器”、“其它PCI桥设备”驱动有问题
    MapReduce实现两表的Join--原理及python和java代码实现
    Calendar类经常用法 日期间的转换 set方法有巨坑
    Java 装饰模式(4.4)
    LoadRunner访问Mysql数据库(转)
    linux下定时执行任务的方法
    docker 常用命令
    Docker exec与Docker attach
    docker容器跨服务器的迁移方式export和save(转)
    cpu时间 / cpu利用率计算
  • 原文地址:https://www.cnblogs.com/cobble19/p/2881283.html
Copyright © 2011-2022 走看看