使用移位运算符做乘除法运算

左乘右除

<<(向左位移) 针对二进制,转换成二进制后(32位)向左边移动n位 ,相当于这个整数乘以2的n次方;

>>(向右位移) 针对二进制,转换成二进制后(32位)向右移动n位,相当于这个整数除以2的n次方,正数高位补0, 正数高位补1;

>>>  (无符号右移)

按二进制形式把所有的数字向右移动对应位数,低位移出(舍弃),高位的空位补零。对于正数来说和带符号右移相同,对于负数来说不同。不管正负数高位都补0

不过这种方式只能用于乘以除以2的n次方,但是他的效率比乘法运算要高;

public class Main {
   public static void main(String[] args){

        int a = 0;
        a = 3 << 1;
        System.out.println("3 << 1 = "+a+" (相当于3*2)");
        a = 3 << 2;
        System.out.println("3 << 2 = "+a+" (相当于3*4)");
        int aa=1;
        aa <<= 2;
        System.out.println("a << 2 = "+aa+" (相当于a*4并将值付给aa)");
        int b = 0;
        b = 10 >> 1;
        System.out.println("10 >> 1 = "+b+" (相当于10/2)");
        b = 10 >> 2;
        System.out.println("10 >> 2 = "+b+" (相当于10/4)");
        System.out.println("3 >> 2 = "+(3>>2));
        System.out.println("3 >> 1 = "+(3>>1));
        System.out.println("5 >> 1 = "+(5>>1));

    }
}

运算结果:

3 << 1 = 6 (相当于3*2)
3 << 2 = 12 (相当于3*4)
a << 2 = 4 (相当于a*4并将值付给aa)
10 >> 1 = 5 (相当于10/2)
10 >> 2 = 2 (相当于10/4)
3 >> 2 = 0
3 >> 1 = 1
5 >> 1 = 2

int level=1; //1代表省级级别,2代表市级级别,3代表县级级别
int userCode = 1 << (3-level); //位运算得到对应的二进制
int access = Integer.parseInt("110", 2); //110代表省市有权限,县级无(将字符串转换成二进制)
boolean canAccess = (userCode & access) > 0; //两个数字进行与运算



以下是三种情况:   
0000 0000 0000 0000 0000 0000 0000 0100 是level代表的值
0000 0000 0000 0000 0000 0000 0000 0110 是设置的权限值
0000 0000 0000 0000 0000 0000 0000 0100=4 省级

0000 0000 0000 0000 0000 0000 0000 0010
0000 0000 0000 0000 0000 0000 0000 0110
0000 0000 0000 0000 0000 0000 0000 0010=2 市级
 

0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0110
0000 0000 0000 0000 0000 0000 0000 0000=0 县级

import com.google.common.collect.Lists;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lwh
 * @create: 2021/7/23 19:14
 * @desc:
 */
@Getter
public enum  ResourcesTypeBitEnum {

    VIDEO("视频", 1<<1, 1),
    EXAM_PAPER("试卷", 1<<2, 2),
    FM("FM", 1<<3, 3),
    CAREER_NEWS("资讯", 1<<4, 4),
    PSYCHOLOGY_REPORT("板报", 1<<5, 5),
    FUN_TEST("测评", 1<<6, 6),
    EXERCISES("专题", 1<<7, 7);

    private String name;
    private Integer bitCode;
    private Integer code;

    ResourcesTypeBitEnum(String name, Integer bitCode, Integer code) {
        this.name = name;
        this.bitCode = bitCode;
        this.code = code;
    }

    /**
     * 所有的科目的枚举
     *
     * @return
     */
    public static Map<Integer, ResourcesTypeBitEnum> getMap() {
        Map<Integer, ResourcesTypeBitEnum> map = new HashMap<>();
        for (ResourcesTypeBitEnum e : ResourcesTypeBitEnum.values()) {
            map.put(e.getCode(), e);
        }
        return map;
    }


    /**
     * 位枚举转换为枚举对象
     *
     * @param bitCode
     * @return
     */
    public static List<ResourcesTypeBitEnum> bitEnum2enumCode(Long bitCode) {
        List<ResourcesTypeBitEnum> result = Lists.newArrayList();
        for (ResourcesTypeBitEnum e : ResourcesTypeBitEnum.values()) {
            if ((bitCode & e.getBitCode()) > 0L) {
                result.add(e);
            }
        }
        return result;
    }


    /**
     * 是否包含指定的类型
     *
     * @param bitCode
     * @param bitEnum
     * @return
     */
    public static boolean bitCodeContains(Long bitCode, ResourcesTypeBitEnum bitEnum) {
        if ((bitCode & bitEnum.getBitCode()) > 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        for (ResourcesTypeBitEnum value : ResourcesTypeBitEnum.values()) {
            System.out.println(value.getBitCode());
        }
    }

}


#mysql按照位图查询, 这种应该是不会走索引的, 说是位图索引能走 有待证实
EXPLAIN SELECT * from t_user where age & 4>0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值