二进制位图

位图

位图能极大地压缩空间

基本实现

 public static class bitMap{
        private long[] bits;//long型是64位的

        public bitMap(int max){
            bits=new long[(max+64)>>6];//右移6位相当于除以64   (max+64)/64
        }
        public void add(int num){
            //num/64-->哪个整数
            //num%64-->num&63
            bits[num>>6]|=(1L<<(num&63));//1L代表long的1(64位)
        }

        public void delete(int num){
            bits[num>>6]&=~(1L<<(num&63));
        }

        public boolean contains(int num){
            return (bits[num>>6]&(1L<<(num&63)))!=0;
        }
    }

二进制运算中,异或运算就是无进位相加

二进制运算的进位信息可用** (a&b)<<1 **得到

位运算实现加减乘除

异或:a^b 同或:(a ^ b)^1

或:| a|=1<<i(可将第i位置为1)

public static int add(int a,int b){
        int sum=0;
        while (b!=0){//一直有进位信息就重复,直到没有进位信息
            sum=a^b;//  a与b的无进位加法
            b=(a&b)<<1;//进位信息
            a=sum;
        }
    }

  //a-b=a+(-b)=a+(~b+1)=a+add(~b,1)=add(a,add(~b,1))

 public static int minus(int a,int b){
        return add(a,add(1,~b));
    }


public static int multi(int a,int b){
        int res=0;
        while (b!=0){
            if ((b&1)!=0){//判断b的末位是否为1,是1,就把a加进去
                res=add(res,a);
            }
            a<<=1;
            b>>>=1;//不带符号右移1位,将比较过的移出去
        }
        return res;
    }



    //a,b都不是系统最小值
    public static int div(int a,int b){
        int x=(a<0)?(add(~a,1)):a;//若a为负数,则将a变为正数
        int y=(b<0)?(add(~b,1)):b;
        int res=0;
        for(int i=30;i>=0;i=minus(i,1)){//i--
            if ((x>>i)>=y){//x右移去够y,y左移可能会出现符号位由0变1,溢出
                res|=1<<i;//将i位置为1
                x=minus(x,y<<i);
            }
        }
        //res为正数,ab异号,a^b为真,res加负号
        return (a>0)^(b>0)?add(~res,1):res;

    }

    public static int divide(int a,int b){
        if(a==Integer.MIN_VALUE&&b==Integer.MIN_VALUE){
            return 1;
        }
        else if (a==Integer.MIN_VALUE){//系统最小值=~(系统最大值+1)
            if (b==add(~1,1)){//b==-1
                return Integer.MAX_VALUE;//本应为Integer.MAX_VALUE+1,但不存在,故约定为MAX_VALUE;
            }
            else {
                int ans=div(add(a,1),b);//a+1的绝对值即MAX_VALUE,再除以b(结果偏小)
                return add(ans,div(minus(a,multi(ans,b)),b));//结果要补偿回来:ans+(a-b*ans)/b
            }

        }
        else if(b==Integer.MIN_VALUE){
            return 0;
        }

        else{
           return div(a,b);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容与0801

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值