package java.io;





/**

 * Utility methods for packing/unpacking primitive values in/out of byte arrays

 * using big-endian byte ordering.

 */

class Bits {





    /*

     * Methods for unpacking primitive values from byte arrays starting at

     * given offsets.

     */





    static boolean getBoolean(byte[] b, int off) {

        return b[off] != 0;

    }





    static char getChar(byte[] b, int off) {

        return (char) ((b[off + 1] & 0xFF) +

                       (b[off] << 8));/*从offer 开始,取出一个char,byte是8位,若对char,byte,short进行移位,自动转换为int,b[off]长度为32位,移位不会溢出。大端:高位数在低地址*/

    }





    static short getShort(byte[] b, int off) {

        return (short) ((b[off + 1] & 0xFF) +

                        (b[off] << 8));

    }





    static int getInt(byte[] b, int off) { //需要截取4个byte,按大端法构成一个int

        return ((b[off + 3] & 0xFF)      ) +

               ((b[off + 2] & 0xFF) <<  8) +

               ((b[off + 1] & 0xFF) << 16) +

               ((b[off    ]       ) << 24);

    }





    static float getFloat(byte[] b, int off) {

        return Float.intBitsToFloat(getInt(b, off));

    }





    static long getLong(byte[] b, int off) {

        return ((b[off + 7] & 0xFFL)      ) +

               ((b[off + 6] & 0xFFL) <<  8) +

               ((b[off + 5] & 0xFFL) << 16) +

               ((b[off + 4] & 0xFFL) << 24) +

               ((b[off + 3] & 0xFFL) << 32) +

               ((b[off + 2] & 0xFFL) << 40) +

               ((b[off + 1] & 0xFFL) << 48) +

               (((long) b[off])      << 56);

    }





    static double getDouble(byte[] b, int off) {

        return Double.longBitsToDouble(getLong(b, off));

    }





    /*

     * Methods for packing primitive values into byte arrays starting at given

     * offsets.

     */





    static void putBoolean(byte[] b, int off, boolean val) {

        b[off] = (byte) (val ? 1 : 0);

    }





    static void putChar(byte[] b, int off, char val) {

        b[off + 1] = (byte) (val      ); //将char转换成byte数组,按大端法:char(16位)的高8位应该在b[off],低8位在b[off+1]中。>>>是无符号右移

        b[off    ] = (byte) (val >>> 8);

    }





    static void putShort(byte[] b, int off, short val) {

        b[off + 1] = (byte) (val      );

        b[off    ] = (byte) (val >>> 8);

    }





    static void putInt(byte[] b, int off, int val) {

        b[off + 3] = (byte) (val       );

        b[off + 2] = (byte) (val >>>  8);

        b[off + 1] = (byte) (val >>> 16);

        b[off    ] = (byte) (val >>> 24);

    }





    static void putFloat(byte[] b, int off, float val) {

        putInt(b, off,  Float.floatToIntBits(val));

    }





    static void putLong(byte[] b, int off, long val) {

        b[off + 7] = (byte) (val       );

        b[off + 6] = (byte) (val >>>  8);

        b[off + 5] = (byte) (val >>> 16);

        b[off + 4] = (byte) (val >>> 24);

        b[off + 3] = (byte) (val >>> 32);

        b[off + 2] = (byte) (val >>> 40);

        b[off + 1] = (byte) (val >>> 48);

        b[off    ] = (byte) (val >>> 56);

    }





    static void putDouble(byte[] b, int off, double val) {

        putLong(b, off, Double.doubleToLongBits(val));

    }

}

Bits.java的更多相关文章

  1. MyCAT报java.lang.OutOfMemoryError: Java heap space

    早上同事反映,mycat又假死了,估计还是内存溢出,查看了一下错误日志. INFO | jvm | // :: | java.lang.OutOfMemoryError: Java heap spac ...

  2. java,maven工程打tar.gz包执行main方法

    一,需要在pom.xml文件添加plugin, 项目目录结构 <build> <plugins> <plugin> <artifactId>maven- ...

  3. java ----> 基础之位运算

    package test.ant; import java.util.Arrays; import java.io.UnsupportedEncodingException; public class ...

  4. 各种 Java Thread State【转载】

    1,线程状态为“waiting for monitor entry”: 意味着它 在等待进入一个临界区 ,所以它在”Entry Set“队列中等待. 此时线程状态一般都是 Blocked: java. ...

  5. 分析Java Thread State

    使用 TDA 工具,看到大量 Java Thread State 的第一反应是: 1,线程状态为“waiting for monitor entry”: 意味着它 在等待进入一个临界区 ,所以它在”E ...

  6. Tomcat 9内存溢出:"http-apr-8080-Acceptor-0" java.lang.OutOfMemoryError: Direct buffer memory

    Tomcat开启了APR模式,而APR模式会使用堆外内存,关于堆内存可从如下链接了解一下:http://blog.csdn.net/zhouhl_cn/article/details/6573213. ...

  7. 在 TDA 工具里看到 Java Thread State 的第一反应是

    转载:http://itindex.net/detail/43158-tda-%E5%B7%A5%E5%85%B7-java   使用 TDA 工具,看到大量 Java Thread State 的第 ...

  8. java启动参数 设置

    JAVA_MEM_OPTS="" BITS=`java -version 2>&1 | grep -i 64-bit` if [ -n "$BITS&quo ...

  9. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

随机推荐

  1. C、C++语言中参数的压栈顺序

    要回答这个问题,就不得不谈一谈printf()函数,printf函数的原型是:printf(const char* format,-) 没错,它是一个不定参函数,那么我们在实际使用中是怎么样知道它的参 ...

  2. docker安装CentOS7及JNI使用相关过程记录

    docker pull centos:centos7(拉取镜像) docker run -itd --name centos-test centos:centos7 (运行容器) docker exe ...

  3. 如何实现一个简易版的 Spring - 如何实现 @Component 注解

    前言 前面两篇文章(如何实现一个简易版的 Spring - 如何实现 Setter 注入.如何实现一个简易版的 Spring - 如何实现 Constructor 注入)介绍的都是基于 XML 配置文 ...

  4. 解决.dll类等文件丢失或出错

    简单暴力: 去官网下载WIN10 SDK 并安装, 将本机的DLL类文件重新刷新一遍. https://developer.microsoft.com/en-US/windows/downloads/ ...

  5. Chrome Canary crashed bug

    Chrome Canary crashed bug Aw, Snap https://support.google.com/chrome/?p=e_awsnap clear cache, 使用隐身模式 ...

  6. js console.log color all in one

    js console.log color all in one console.log color Chrome console.log 语法 / grammar %c, %s, css style ...

  7. Node.js & ES Modules & Jest

    Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...

  8. Azure & Serverless

    Azure & Serverless https://azure.microsoft.com/en-us/get-started/webinar/on-demand/ blob:https:/ ...

  9. flutter web in action

    flutter web in action flutter for web https://flutter.dev/web https://flutter.dev/docs/get-started/w ...

  10. how to recursively all files in a folder with sudo permissions in macOS

    how to recursively all files in a folder with sudo permissions in macOS write bug OK sudo chmod 777 ...