int型32位压缩

int型32位压缩

之前看到有个人问如何读取文件,文件中的数字为0~1024的数字,int型存储是默认32位。想以12位进行压缩。
利用晚上时间写了个main方法进行,顺便进行了测试。原理很简单就是每隔12位存储,多余就补位。
以前对java的位移不是很了解,因为不是本专业的,后来进入it圈发展。自己写的玩的。如果谁能用到就拿去看看吧。代码应该可以优化,由于比较懒就算了。



    public static void main(String[] args) {

        int[] is = {1, 3, 4, 5, 7, 9, 11, 1, 1, 5, 1, 1, 1};
        List<Integer> newis = new ArrayList<Integer>();
        int temp1 = 0;
        int tempi = 0; // 余量
        int tempj = 0; // 需量
        int tempm = 0; // 总位数
        boolean canadd = false;
        for (int i = 0; i < is.length; i++) {

            if (tempj != 0) {
                i--;
                temp1 = is[i] >> tempi;
                tempm = tempm + tempj;
                tempj = 0;
                tempi = 0;
                continue;
            }

            if (tempm % 32 == 0) {
                temp1 = is[i];
                tempm+=12;
                if (i == is.length - 1) {
                    newis.add(is[i]);
                }
                continue;
            }

            if (tempm + 12 >= (newis.size() + 1)*32) {
                tempi = 32 - tempm%32;
                tempj = 12 - tempi;

                temp1 = temp1 | is[i] << (32 - tempi);
                tempm = tempm + tempi;
                canadd = true;
            } else {
                temp1 = temp1 | is[i] << tempm % 32;
                tempm += 12;

            }


            if (canadd) {
                newis.add(temp1);
                canadd = false; 
            } else if (i == is.length - 1) {
                newis.add(temp1);
            }


        }

        // 解压过程
        for (int i = 0; i < newis.size(); i++) {
            // 自己写吧
        }


    }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值