当前位置:网站首页>Bit mask of bit operation
Bit mask of bit operation
2022-07-05 05:36:00 【Falling spring is only inadvertently】
Bitmask
Use bit mask to realize set
Operation set is the most important application of bit mask . In this application ,N Bit integer variables indicate that you can have 0 To A-l Set of integer elements . To judge an element i Is included in the collection , Just look at the representation 2 i 2^i 2i Whether the bit of has been turned on . for example , Can have 6 Collection of elements {1,4,5,6,7,9} The whole number of is 754 Shown by the following 、
21+24+25+26+27+29=101110010 = 754
Pizza shop example
Suppose a pizza shop allows customers to choose their own ingredients , Now I want to write a ordering system for this store . This pizza shop has 0 To 19 This is the number one 20 Ingredients , You can choose to add / Don't add . that ,1 The message of a pizza is 20 Collection of elements , So it can be represented by a bit mask . Of course , You can also use a size of 20 An array of Boolean types . however , Using bit mask can improve the speed of set operation through a variety of bit operations .
Find empty sets and compact sets
An empty set is 0 了 . Compact set
int fullPizza = (1<<20) - 1
take 1<< 20 Expressed in binary form , It's just one. 1 And noodles 20 individual 0 The integer of . Subtract one , You can get 20 Bits are turned on (1) The numerical
Additive elements
toppings |= (1<<p);
take 1 Move to the left p You get the first p Bit open integer
Confirm whether the element is included
if( toppings & (1<<p)) cout<<" in it!"<<endl;
& That only toppings Of the i Only when the bit is turned on can it be true
Remove elements
toppings &= ~(1<<p);
Switch elements
It turns out to be 0 becomes 1, yes 1 becomes 0
toppings ^= (1<<p);
Operation on two sets
int added = (a|b); //a and b A collection of
int intersection = (a&b);// a and b Intersection
int removed = (a&~b); // from a subtract b The difference between the set
int toggled = (a^b); // Only be a and b A set in Contains a collection of elements .
Find the smallest element
That is to say, there are several current low positions 0?
int firstTopping = (toppings & -topping);
Delete the smallest element
toppings &= (topping - 1);
Traversing subsets
for(int subset = pizza; subset;subset = ((subset-1)&pizza)){
// subset yes pizza Subset
}
边栏推荐
- Little known skills of Task Manager
- Haut OJ 1350: choice sends candy
- 利用HashMap实现简单缓存
- Hang wait lock vs spin lock (where both are used)
- Using HashMap to realize simple cache
- 每日一题-搜索二维矩阵ps二维数组的查找
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
- ALU逻辑运算单元
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- Haut OJ 1243: simple mathematical problems
猜你喜欢

Gbase database helps the development of digital finance in the Bay Area

AtCoder Grand Contest 013 E - Placing Squares

全排列的代码 (递归写法)

Sword finger offer 53 - I. find the number I in the sorted array

Reader writer model

【Jailhouse 文章】Jailhouse Hypervisor

读者写者模型

Fried chicken nuggets and fifa22

Codeforces round 712 (Div. 2) d. 3-coloring (construction)
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces
随机推荐
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Sword finger offer 06 Print linked list from beginning to end
CF1634 F. Fibonacci Additions
注解与反射
Summary of Haut OJ 2021 freshman week
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
High precision subtraction
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Web APIs DOM node
Cluster script of data warehouse project
第六章 数据流建模—课后习题
Yolov5 ajouter un mécanisme d'attention
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
数仓项目的集群脚本
全排列的代码 (递归写法)
【实战技能】非技术背景经理的技术管理
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
CF1637E Best Pair
To be continued] [UE4 notes] L4 object editing
Over fitting and regularization