当前位置:网站首页>Don't talk about the source code, talk about the practical application of bit operation
Don't talk about the source code, talk about the practical application of bit operation
2022-07-22 12:02:00 【YYniannian】
Bit operations are java The basic knowledge in the comparison , The calculation speed of bit operation is very fast ,jdk Many source codes use bit operations ( for example ArrayList,HashMap)
.
But in the process of daily development , We seldom use bit operations . Compared with its advantages of computing speed , Its readability is not high ” shortcoming “ It seems to be more prominent for ordinary business development .
We prefer to use it directly +,-,*,/
These direct operators .
This article will introduce an application scenario of bit operation in business , I hope I can help you , If there is anything wrong , Welcome to correct .
Introduce the basis of standard mismatch operation , If you are not familiar with the basic concepts of bit operation , Please help yourself first google understand .
One 、 Conventional business marking ideas
In the process of daily business development , Business models are often marked (true/false) The needs of .
for example :
true Boys ,false Girls say
true It means that there are discounts for participating in the event ,false Indicates that you cannot participate in the event offer
true Super Administrator ,false For ordinary users
.....
Generally speaking, for this kind of demand , We usually use a separate field to represent Domain The state of . As the business grows , More and more logic needs to be marked . Corresponding to Domain/ The fields identified in the table structure will also continue to rise .
@Data
public class User {
private Long userId;
private String userName;
private boolean canLogin;
private boolean isMale;
private boolean isAdmin;
....
}
Copy code
Two 、 Bit operation optimization logic
In order to solve this non A namely B The expansion problem of field identification , We can use bit operations to integrate these fields .
For example, as shown above User Domain, We integrate canLogin,isMale,isAdmin For a field , Think of it as a feature identification ( Set field to featureTag
).
@Data
public class User {
private Long userId;
private String userName;
private int featureTag;
}
Copy code
We can define one feature The enumeration of defines what each bit identifies
public enum UserFeatureEnum{
CAN_LOGIN(0),
IS_MALE(1);
IS_ADMIN(2);
private int bit;
UserFeatureEnum(int bit) {
this.bit = bit;
}
public int getValue() {
return this.bit;
}
}
Copy code
Then we can pass BitUtil Come on check The status of a feature of the current user
public class BitUtil {
public static int setBit(int options, int bitIndex) {
int val = 1 << bitIndex;
return options | val;
}
public static int unsetBit(int options, int bitIndex) {
int val = 1 << bitIndex;
return options & (~ val);
}
public static boolean isBitSetted(int options, int bitIndex) {
int val = 1 << bitIndex;
return (options & val) == val;
}
public static boolean isBitUnSetted(int options, int bitIndex) {
int val = 1 << bitIndex;
return (options & val) == 0;
}
}
Copy code
So if we check user Whether men can do this
boolean isMale = BitUtil.isBitSetted(user.getfeatureTag(),UserFeatureEnum.IS_MALE.getValue());
Copy code
Set the current user as female
user.setfeatureTag(BitUtil.unsetBit(user.getfeatureTag(),UserFeatureEnum.IS_MALE.getValue()))
Copy code
Set the current user as male
user.setfeatureTag(BitUtil.setBit(user.getfeatureTag(),UserFeatureEnum.IS_MALE.getValue()))
Copy code
In this way, we have merged the expanding flag fields , The database can also be identified with only one field Domain The multiple feature.
If you need to add one later feature when , There is no need to modify the table structure , Just add an enumeration value and setting 、 Just judge logic .
Like online projects , Changing the table structure will go a long way DBA Approval process
3、 ... and 、 Comparison of advantages and disadvantages
- Compared with conventional business design , Bit operation is more prominent in space saving and scalability , But the readability is relatively poor .
- stay DB Search on , Conventional design can filter fields directly as conditions , Bit operation requires a bit operation on the condition .
- stay ES Search on , Conventional design can filter fields directly as conditions , Bit operation is not supported , One field needs to be redundant , If Domain Our storage structure is ES, also feature Fields have retrieval requirements , It's better to follow the conventional design .
- If it involves the function of interacting with the client , edition A Of client Supported features , In version B Can't support . Because the client is installed locally , It is impossible to force users to upgrade all the time . So you can use a feature Field , Identify some features supported by the current client , The server parses the returned result . The request parameters are clear and simple , High dynamic .
Four 、 Last
If there is anything wrong in the article , Welcome to correct , Writing is not easy , Give me some advice. , mua ~
边栏推荐
- 9.5~10.5 GHz频段室内离体信道的测量与建模
- Library management system based on jsp+servlet+mysql+bootstrap+css
- 第01篇:分布式注册中心
- 【快速上手教程5】疯壳·开源编队无人机-飞控固件烧写
- Web3 can't escape from the Wuzhishan of these old giants no matter how powerful it is
- 如何优雅的整合定时批量任务(荣耀典藏版)
- PyQt5制作一个精致的视频播放器*附源码
- baseline
- News blog publishing system based on jsp+servlet+mysql
- Multithreading and high concurrency day09
猜你喜欢
Explain the data structure of redis in detail
IP第十二天笔记
如何优雅的整合定时批量任务(荣耀典藏版)
J9 number theory: what is Dao? What is the origin of Dao
Partner cloud Dai Zhikang: how to use low code to improve R & D and it efficiency
从腾讯外包月薪5K到转岗正式员工月薪15K,这168天的心酸谁能懂...
C language explanation series -- Explanation of goto sentences and simple exercises of circular sentences
OpenOffice startup and auto startup settings
baseline
[hero planet July training leetcode problem solving daily] 21st pile
随机推荐
Pyqt5 makes an exquisite video player * with source code
Measurement and modeling of indoor in vitro channel in 9.5 ~ 10.5 GHz band
小熊派BearPi-HM_Micro_Small之Hello_World
Hcip day 5
EN 1504-5 products for protection and repair of concrete structures concrete spraying - CE certification
师傅教你~LNMP源码搭建
为什么都说软件测试岗是巨坑?10年测试人告诉你千万别上当
Classic network architecture learning RESNET
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
An access scheme based on dynamic allocation of random access opportunity
Explain the data structure of redis in detail
Didi was fined 8.026 billion yuan. Musk made toys with Barbie dolls. Dall-e invited millions of people to join the test. Today, more new things are here
EN 1504-4: structural bonding of concrete structure products - CE certification
Part 02: distributed load balancing
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
Titre 01: Registre distribué
J9 number theory: what is Dao? What is the origin of Dao
电缆故障监测vr虚拟仿真教学软件的好处
Screen adaptation method of mobile web app (summary)
国际标准ISO/IEC 30144: 2020在智能变电站辅助监测中的应用研究