当前位置:网站首页>Bit operation marks multiple switches with one parameter
Bit operation marks multiple switches with one parameter
2022-06-11 05:31:00 【Trace】
For example, a mobile phone , There may be the following flag bits :
flag = Mobile phone properties
- Whether the screen is damaged case1
- Whether the battery is charged case2
- Is there a camera case3
- Can I make a phone call case4
- Whether it's an Apple phone case5
- Whether it is within the warranty period case6
Normal people may use bool Type to store n Multiple parameters , But you can also use a int Or is it long To store , stay java in int Occupy 4 Bytes each byte accounts for 8 A total of 32 position , That is to say, it can store 32 The marks are , save 32 individual bool value .
Use bit operations to operate on a int Tag for value ,int The size of the value itself makes no sense , Only focus on the binary corresponding position ,0 For unmarked ,1 Means marked .
to flag Add tag
The formula :
flag |= case1
flag |= case2
flag |= case2|case2
Binary computing Demo :
flag = 0000 0000
case1 = 0000 0001
case2 = 0000 0100
Or after operation ↓
flag = 0000 0101
Judgment marks
The formula :
// Single mark judgment
bool = (flag & case1) != 0
// Composite mark judgment
bool = (flag & control) != 0
Binary computing Demo :
flag = 0000 0000
case1 = 0000 0001
And after the operation ↓
flag = 0000 0000
Delete tag
The formula :
flag &= ~case1
Binary computing Demo :
case1 = 0000 0001
~case1 = 1111 1110
flag = 0000 0001
flag&=~case1 = 0000 0000
Tips
Single meaning case No similar 0010 0001, Two marker bits appear , Unless it's a composite case, in addition case The positions between them cannot conflict .
java Can be defined by shift method case:
int flag = 0; // 0000 0000 0000 0000 0000 0000 0000 0000
int case1 = 1; // 0000 0000 0000 0000 0000 0000 0000 0001
int case2 = 1 << 1; // 0000 0000 0000 0000 0000 0000 0000 0010
int case3 = 1 << 2; // 0000 0000 0000 0000 0000 0000 0000 0100
int case4 = 1 << 3; // 0000 0000 0000 0000 0000 0000 0000 1000
Sample code
int flag = 0 << 0;
int case1 = 1 << 0;
int case2 = 1 << 1;
int case3 = 1 << 2;
int case4 = 1 << 31;
// To mark
flag |= case1;
flag |= case2;
flag |= case3;
flag |= case4;
// Verification mark
Assert.assertTrue((flag & case1) != 0);
Assert.assertTrue((flag & case2) != 0);
Assert.assertTrue((flag & case3) != 0);
Assert.assertTrue((flag & case4) != 0);
// Delete tag
flag &= ~case1;
flag &= ~case2;
flag &= ~case3;
flag &= ~case4;
// Verification mark
Assert.assertFalse((flag & case1) != 0);
Assert.assertFalse((flag & case2) != 0);
Assert.assertFalse((flag & case3) != 0);
Assert.assertFalse((flag & case4) != 0);
边栏推荐
- 49. 字母异位词分组
- Handle double quotation mark escape in JSON string
- Share | guide language image pre training to achieve unified visual language understanding and generation
- 初步了解多任务学习
- 2021-04-19
- Combing route - Compaction Technology
- 22. Generate parentheses
- 35.搜索插入位置
- SwiftUI: Navigation all know
- In the future, how long will robots or AI have human creativity?
猜你喜欢

Introduction to coordinate system in navigation system

Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM

KD-Tree and LSH

1.使用阿里云对象OSS(初级)

Stone game -- leetcode practice

Analyzing while experimenting - memory leakage caused by non static inner classes

SQLite installation and configuration tutorial +navicat operation
![[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision](/img/89/66c30ea8d7969fef76785da1627ce5.jpg)
[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision

Vins fusion GPS fusion part

mysql字符串转数组,合并结果集,转成数组
随机推荐
How much current can PCB wiring carry
NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
White Gaussian noise (WGN)
Thesis 𞓜 jointly pre training transformers on unpaired images and text
(十五)红外通信
Basics of customized view
Section II: structural composition characteristics of asphalt pavement (2) structural layer and performance requirements
Combing route - Compaction Technology
Technology | image motion drive interpretation of first order motion model
jvm调优五:jvm调优工具和调优实战
MySQL string to array, merge result set, and convert to array
[go deep into kotlin] - get to know flow for the first time
Share | defend against physically realizable image classification attacks
截取文件扩展名
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
Inventory | ICLR 2022 migration learning, visual transformer article summary
MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
C (I) C basic grammar all in one
WinForm (I) introduction to WinForm and use of basic controls
【深入kotlin】 - Flow 进阶