计算二进制1出现的次数,bigmap


/**
 * User: ldj
 * Date: 2023/4/12
 * Time: 11:08
 * Description: No Description
 */
public class BinaryCountOne {
    public static void main(String[] args) {
        int number = 10; //1010
        int count = 0;
        while (number != 0) {
            //number % 2 第一次求余的结果就是二进制的最右边的数(余数倒写)
            if (number % 2 == 1) {
                count++;
            }
            number >>>= 1;
        }
        System.out.println(count);
    }
}

 变形一下

    public static void main(String[] args) {
        int number = 26;//11010
        int count = 0;
        while (number != 0) {
            if ((number & 1) == 1) {
                count++;
            }
            number >>>= 1;
        }
        System.out.println(count);
    }

后续补充:签到功能

    /**
     * 统计签到
     * @return
     */
    @GetMapping("/sign/count")
    public Result signCount(@RequestParam(value = "continuous",required = false) Boolean continuous) {
        return userService.signCount(continuous);
    }


    /**
     * 签到
     */
    @Override
    public Result sign() {
        Long userId = UserHolder.getUser().getId();
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String redisKey = RedisConstant.REDIS_SIGN_PREFIX + userId + ":" + date.substring(0, 6);

        //获取今天是本月第几天
        int dayOfMonth = Integer.parseInt(date.substring(6, 8));
        //位标是固定有序的,0位置是第一天,1位置第二天..... 没签到默认是0,  (true:1,false:0)
        stringRedisTemplate.opsForValue().setBit(redisKey, dayOfMonth - 1, true);

        return Result.ok();
    }

    /**
     * 统计签到
     */
    @Override
    public Result signCount(Boolean continuous) {
        Long userId = UserHolder.getUser().getId();
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String redisKey = RedisConstant.REDIS_SIGN_PREFIX + userId + ":" + date.substring(0, 6);

        //获取今天是本月第几天
        int dayOfMonth = Integer.parseInt(date.substring(6, 8));

        //获取本月截至今天的签到记录
        List subCommands = new ArrayList<>();
        List result = stringRedisTemplate.opsForValue()
                .bitField(redisKey,
                        //忽略十进制数正负符号,从小标0开始截取dayOfMonth bit位
                        //BITFIELD redisKey GET u5 0
                        BitFieldSubCommands.create()
                                .get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0));

        if (CollectionUtils.isEmpty(result)) {
            return Result.ok(0);
        }

        //只传一个get请求
        Long number = result.get(0);
        if (number == null || number == 0) {
            return Result.ok(0);
        }

        int count = 0;
        //开关:从今天往后算,连续签到次数
        if (continuous != null && continuous) {
            //连续签到功能
            while (true) {
                if ((number & 1) == 0) {
                    //等于0,说明未签到
                    break;
                } else {
                    //不等于0,说明已签到
                    count++;
                }
                number >>>= 1;
            }
        }else {
            //从本月1号开始,截至今天总共签到次数
            while (number != 0) {
                //number % 2 第一次求余的结果就是二进制的最右边的数(余数倒写)
                if ((number & 1) == 1) {
                    count++;
                }
                number >>>= 1;
            }
        }

        return Result.ok(count);
    }

你可能感兴趣的:(java,redis,bigmap)