当前位置:网站首页>Bit operation topic
Bit operation topic
2022-06-10 19:51:00 【Starlight technician】
Bit operation topic
1. Bitmap solves the occurrence of numbers in a certain range , Save a lot of time

- Topic analysis
Use hash method to solve , The general idea is to apply for a hash table , Put the hash value of each number in the file after the hash function into the corresponding hash grid ( Add one to the corresponding position value of the hash table ); After traversing all the numbers in the file , Observe Hashtable , The elements in the hash table are 0 The number corresponding to the position of is the number that does not appear ;
But in that case , You need to apply for a length of 232 Hash table for , Each position is an unsigned integer type uint(4 Bytes ,32 position , The scope is 0 ~ 232-1, It can meet the requirement that all numbers in the file are the same ); This limits memory 10M, It must not be enough ; Let alone limit memory 10kB;
If memory is limited 3KB, that 3KB=3000byte,3000/4 = 750 Unsigned integers ; So the application length is 512 Hash table for ; take 0 ~ 232 The data is divided into 512 Share , Then traverse the data in the file , Add the corresponding hash table position element 1; When the number in the file is traversed , Observe the size of each element in the hash table , Find out what has not been achieved 232 / 512 The location of , This explanation 512 Of the data , This part of the data is missing , The missing data we are looking for must be in this part ; Then iterate over the file elements again , Figures not in this section are ignored , In this part of the figure press the new 512 Block principle storage ; Proceed in sequence , You can go to 3KB In memory ;
2. Bit operation to find the maximum value
The bit operation compares the maximum of two numbers
- Ideas 1
- code
int flip(int n)
{
return n ^ 1;
}
int sign(int n)
{
return flip((n >> 31) & 1);
}
int get_max1(int a, int b)
{
int c = a - b;
int scA = sign(c);//a-b For non negative ,scA=1;a-b Negative ,scA=0
int scB = flip(scA);// a - b For non negative ,scB = 0; a - b Negative ,scB = 1
return a * scA + b * scB;
}
Ideas 1 problem : When a-b When it overflows , The result is not right
- Ideas 2
- code2
int get_max2(int a, int b)
{
int c = a - b;
int sa = sign(a);//a For non negative ,sa = 1;
int sb = sign(b);//b For non negative ,sa = 1
int sc = sign(c);
int difSab = sa ^ sb; //a and b The symbols are different ,difSab =1;
int samSab = flip(difSab);//a and b The symbols are the same ,samSab =1;
int returnA = difSab * sa + samSab * sc;//a,b Same symbols and a>b perhaps
//a,b Symbols are different and a>0 Back when A
int returnB = flip(returnA);
return a * returnA + b * returnB;
}
doubt : How to determine a,b The symbols are different ,a>0 When ,sign by 1
3. Determine whether an integer is 2 The power of
Ideas 1
If an array is judged to be 2 Of n The next power ; Then move the number to the right n position , Determine whether the end of this time is bit 1, If it is 1, It means 2 Of n The next powerIdeas 2
The number if bit 2 The power of , Then there is only one binary form 1;
If x&(x-1)=0, that x Namely 2 The power of ;code
bool is4pow(int n)
{
return (n&(n-1))==0;
}
4. Determine whether an integer is 4 The power of
- Judge X Is it 2 The power of , That is, there is only one binary form 1
2) If X by 2 The power of , Further judgment is needed X Is it 4 The power of
If X by 4 The power of , In binary form , It could be in the 0,2,4,6,8 There is only one even number 1, There is no one on the odd bit 1;X&(0101010101…0101)!=0, explain X by 4 The power of
- code
bool is4pow(int n)
{
return (n&(n-1))==0 & (n&0x5555)!=0;
}
Use bit operation to add, subtract, multiply and divide two numbers

https://www.bilibili.com/video/BV13g41157hK?p=16
An hour 40 branch
边栏推荐
- Tencent libco collaboration open source library source code analysis full series summary blog
- Lingo12 software download and lingo language introduction resources
- Summary of "performance test" of special test
- [advanced C language] data storage [part I] [ten thousand words summary]
- The team of nature biotechnol | Li Jiayang / Yu Hong used the tile deletion strategy to break the linkage of traits and break through the bottleneck of rice yield
- 马斯克称自己不喜欢做CEO,更想做技术和设计;吴恩达的《机器学习》课程即将关闭注册|极客头条
- 领域驱动设计(六) - 架构设计浅谈
- 【C语言进阶】指针的进阶【中篇】
- 全数字时代,企业IT如何完成转型?
- VR全景如何应用在家装中?体验真实的家装效果
猜你喜欢

My first work: tensorflow2 x

When the college entrance examination is opened, VR panorama can see the test site in this way

Easily learn pytoch full convolution neural network to realize expression recognition

In the all digital era, how can enterprise it complete transformation?

高考结束,VR全景云游为你展现景区的美好风光

Source code analysis and practical testing openfeign load balancing

禁止摆烂,软件测试工程师从初级到高级进阶指南,助你一路晋升
![[web] personal homepage web homework](/img/16/90b7b559e43e7cd6d5e32865eb0c00.png)
[web] personal homepage web homework "timetable", "photo album" and "message board"

Morris traversal of binary tree

DDD落地实践复盘 - 记理论培训&事件风暴
随机推荐
高考开启,VR全景可以这样看考点
2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】
How is it safe for individuals to invest in financial management?
【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
专项测试之「 性能测试」总结
Ranked first in China's SDN (software) market share for six consecutive years
MATLAB 根据任意角度、取样点数(分辨率)、位置、大小画椭圆代码
Dock/rancher2 deploy redis:5.0.9
Apicloud visual development novice graphic tutorial
Design and implementation of SSM based traffic metering cloud system Rar (thesis + project source code)
Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
DataScience&ML:金融科技领域之风控之风控指标/字段相关概念、口径逻辑之详细攻略
仅需三步学会使用低代码ThingJS与森数据DIX数据对接
An error row size too large (& gt; 8126) occurs when MySQL's MyISAM engine switches to InnoDB
How do various embedded functions in VR panoramic works be achieved?
DDD落地实践复盘 - 记理论培训&事件风暴