zoukankan      html  css  js  c++  java
  • 一个压缩算法

    package algorithm;
    
    
    public class Zigzag {
    	// 对于所有整数,符号位放最后一位。 整数数值位不变;负数除符号位外,全部取反。
    	public int int_to_compInt(int n) {
    		return (n << 1) ^ (n >> 31);
    	}
    
    	//符号位迁移至第一位。正数数值位不变。负数数值位取反。
    	public int comInt_to_int(int n) {
    		return (n >>> 1) ^ -(n & 1);
    
    	}
    
    	public byte[] write_to_buffer(int comInt) 
    	{
    		int tmp=comInt;
    		int len=0;
    		while(tmp!=0) 
    		{
    			tmp=tmp>>>7;
    			len++;
    		}
    		tmp=comInt;
    		byte[] buffer=new byte[len];
    		for (int i=0 ;i< len-1;i++) {
    			
    			buffer[i]=(byte)((tmp & 0x7f)|0x80);
    			tmp=tmp>>>7;
    		}
    		buffer[len-1]=(byte)(tmp & 0x7f);
    		return buffer;
    	}
    	
    	public int read_from_buffer(byte[] buffer)
    	{
    		int result=0;
    		int len=buffer.length;
    		for(int i=len-1;i>=0;i--) 
    		{
    			result=(result<<7)^(buffer[i]&0x7f);
    		}
    		return result;
    	}
    	public static void main(String[] args) 
    	{
    		Zigzag zigzag = new Zigzag();
    		int zigzagInt = zigzag.int_to_compInt(-589);
    		byte[] comptBytes=zigzag.write_to_buffer(zigzagInt);
    		int readBytes=zigzag.read_from_buffer(comptBytes);
    		int ori=zigzag.comInt_to_int(readBytes);
    		System.out.println(ori);
    	}
    }
    

      

  • 相关阅读:
    boost库:函数对象
    boost库:智能指针
    linux 查看和修改文件时间
    linux正则表达式
    UVA
    UVA
    UVA
    UVA
    UVA
    对JavaScript的认识?
  • 原文地址:https://www.cnblogs.com/wfq9330/p/9431507.html
Copyright © 2011-2022 走看看