什么是Java中最有效的方式来位打包成字节[]和读回? [英] What is the most efficient way in Java to pack bits into byte[] and read it back?

查看:217
本文介绍了什么是Java中最有效的方式来位打包成字节[]和读回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用这两个功能包和一个字节数组读取位。想知道如果任何人有更好的想法或更快的方式做到这一点?

编辑的程序有一些更多的优化,并提出了一些计算。目前100mil的PUT和GET现在大约需要12秒而不是16秒。

如果有人使用当前code确保传递把该值是一个正数,因为它的预期无符号数下来。如果有兴趣我可以把符号和无符号版本。

 类BitData
{
    静态无效认沽(字节数据[],最终诠释BitOffset,诠释NUMBITS,最终整数值)
    {
        最终长valLong =(价值及((1L<< NUMBITS)-1L));
        INT posByte = BitOffset>→3;
        INT posBit = BitOffset和7;
        INT valByte;
        INT ModifyBits;        长左值;
        INT LeftShift;
        ModifyBits = 8 posBit;
        如果(NUMBITS< ModifyBits)ModifyBits = NUM​​BITS;
        LeftShift =(8- posBit-ModifyBits);
        而(真)
        {
            valByte =数据[posByte]
            如果(ModifyBits == 8)
            {
                左值= valLong&所述;≤(32 NUMBITS)GT;>(24);
                数据[posByte] =(字节)左值;
            }
            其他
            {
                左值= valLong&所述;≤(32 NUMBITS)GT;>(32 ModifyBits)所述;&下; LeftShift;
                数据[posByte] =(字节)((valByte&放大器;〜(((1 <<;&下; ModifyBits)-1)下;&下; LeftShift))|左值);
            }
            NumBits- = ModifyBits;
            如果(NUMBITS == 0)打破;
            posByte ++;
            ModifyBits = 8;
            如果(NUMBITS&LT; ModifyBits)
            {
                ModifyBits = NUM​​BITS;
                LeftShift =(8- ModifyBits);
            }
        }
    }    静态INT调用getInt(字节数据[],最终诠释BitOffset,诠释NUMBITS)
    {
        INT posByte = BitOffset&GT;→3;
        INT posBit = BitOffset和7;
        长值= 0;
        INT ModifyBits;
        INT valByte;
        INT LeftShift;
        ModifyBits = 8 posBit;
        如果(NUMBITS&LT; ModifyBits)ModifyBits = NUM​​BITS;
        LeftShift =(8- posBit-ModifyBits);
        而(真)
        {
            valByte =数据[posByte]放大器; 0xFF的;
            如果(ModifyBits == 8)值+ = valByte;
            别的值+ =(valByte及((1 <<;&下; ModifyBits)-1)下;&下; LeftShift)GT;&GT;左移;
            NumBits- = ModifyBits;
            如果(NUMBITS == 0)打破;
            posByte ++;
            ModifyBits = 8;
            如果(NUMBITS&LT; ModifyBits)
            {
                ModifyBits = NUM​​BITS;
                LeftShift =(8- ModifyBits);
            }
            价值与LT;&LT; = ModifyBits;        }
        回报(INT)值;
    }
}


解决方案

一个完全不同的路线将是确定所有可能组合的静态表,并执行查找,而不是计算的结果各一次。我认为是他们如何做到这一点的密码。数组[我]×3应该比NUMBITS位运算速度要快得多。它会占据虽然有些堆。

I currently use these two functions to pack and read bits in a byte array. Wondering if anybody has any better ideas or faster ways to do it?

Edited the program with a few more optimization and tabled a few calculations. Currently 100mil Put and Get takes about 12 secs instead of 16 secs now.

If anybody is using the current code make sure the value passed in to Put is a positive number as it's expecting unsigned numbers coming down. If there is interest I can put up signed and unsigned versions.

class BitData
{
    static void Put(byte Data[], final int BitOffset, int NumBits, final int Value)
    {
        final long valLong=(Value&((1L<<NumBits)-1L));
        int posByte=BitOffset>>3;
        int posBit=BitOffset&7;
        int valByte;
        int ModifyBits;

        long lValue;
        int LeftShift;
        ModifyBits=8-posBit;
        if(NumBits<ModifyBits) ModifyBits=NumBits;
        LeftShift=(8-posBit-ModifyBits);
        while(true)
        {
            valByte = Data[posByte];
            if(ModifyBits==8)
            {
                lValue=valLong<<(32-NumBits)>>(24);
                Data[posByte]=(byte)lValue;
            }
            else
            {   
                lValue=valLong<<(32-NumBits)>>(32-ModifyBits)<<LeftShift;
                Data[posByte]=(byte)((valByte & ~(((1<<ModifyBits)-1) << LeftShift)) | lValue);
            }
            NumBits-=ModifyBits;
            if(NumBits==0) break;
            posByte++;          
            ModifyBits=8;
            if(NumBits<ModifyBits) 
            {
                ModifyBits=NumBits;
                LeftShift=(8-ModifyBits);
            }
        }
    }

    static int GetInt(byte Data[], final int BitOffset, int NumBits)
    {       
        int posByte=BitOffset>>3;
        int posBit=BitOffset&7;


        long Value=0;
        int ModifyBits;
        int valByte;
        int LeftShift;
        ModifyBits=8-posBit;
        if(NumBits<ModifyBits) ModifyBits=NumBits;
        LeftShift=(8-posBit-ModifyBits);
        while(true)
        {
            valByte = Data[posByte] & 0xff;
            if(ModifyBits==8) Value+=valByte;
            else Value+=(valByte & ((1<<ModifyBits)-1) << LeftShift) >> LeftShift;              
            NumBits-=ModifyBits;
            if(NumBits==0) break;
            posByte++;
            ModifyBits=8;
            if(NumBits<ModifyBits) 
            {
                ModifyBits=NumBits;
                LeftShift=(8-ModifyBits);
            }
            Value<<=ModifyBits;

        }
        return (int)Value;
    }
}

解决方案

A totally different route would be to define static table of all possible combinations and perform a lookup instead of calculating results each time. I think thats how they do it in cryptography. array[i] x 3 should be much faster than numBits bitwise operations. It will occupy some heap though.

这篇关于什么是Java中最有效的方式来位打包成字节[]和读回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆