java.io(一):Bits

本文探讨了Java.IO中的Bits工具类,它提供用于在大端字节序的字节数组中处理原始值的方法。文章讲解了大端模式的概念,即高位字节位于低地址,低位字节位于高地址,同时也提到了小端模式作为对比。
摘要由CSDN通过智能技术生成

java.io

Bits

Bits类
使用大端字节顺序在字节数组中打包/解包原始值的实用程序方法。

big-endian:大端模式:指数据的高位保存在内存的低地址处,数据的低位保存在内存的高地址处。
小端模式:数据的高位保存在内存的高地址处,数据的低位保存在低地址处。

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.
     * 用于从给定偏移量开始的字节数组中解包原始值的方法。  
     * 首先分析第一个方法,仅仅用于获取byte数组中off坐标下是否存在不为0的数据,因为boolean类型只占1位,即1bit
     */
    static boolean getBoolean(byte[] b, int off) {
   
        return b[off] != 0;
    }
	/*
	 *1byte = 8bit。1字节=8位
	 *1char = 16位(bit)
	 */
    static char getChar(byte[] b, int off) {
   
        return (char) ((b[off + 1] & 0xFF) +
                       (b[off] << 8));
    }
	/*
	 *1byte = 8bit。
	 *1short = 16位(bit)
	 */
    static short getShort(byte[] b, int off) {
   
        return (short) ((b[off + 1] & 0xFF) +
                        (b[off] << 8));
    }
	/*
	 *1byte = 8bit。
	 *1short = 32位(bit)
	 */
    static int getInt(byte[] b, int off) {
   
        return ((b[o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值