

实例操作
public Result sign() {
Long id = UserHolder.getUser().getId();
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
int dayOfMonth = now.getDayOfMonth() ;
String key = USER_SIGN_KEY + id + format;
stringRedisTemplate.opsForValue().setBit(key, dayOfMonth-1, true);
return Result.ok();
}
@Override
public Result signCount() {
Long id = UserHolder.getUser().getId();
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
int dayOfMonth = now.getDayOfMonth();
String key = USER_SIGN_KEY + id + format;
List<Long> longs = stringRedisTemplate.opsForValue().bitField(key,
BitFieldSubCommands.create()
.get(BitFieldSubCommands
.BitFieldType
.unsigned(dayOfMonth))//unsigned无符号
.valueAt(0)//开始位置
);
if (longs == null || longs.isEmpty()) {
return Result.ok(0);
}
Long aLong;
aLong = longs.get(0);
if (aLong == null || aLong == 0) {
Result.ok(0);
}
int count = 0;
while (true) {
if ((aLong & 1) == 0) {
break;
} else {
count++;
}
aLong >>>= 1;//无符号位移
}
return Result.ok(count);
}