当前位置:网站首页>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
- 2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
- 仅需三步学会使用低代码ThingJS与森数据DIX数据对接
- SAR图像聚焦质量评价插件
- Which school do you choose after the college entrance examination? VR panoramic campus all-round display
- DataScience&ML:金融科技领域之风控之风控指标/字段相关概念、口径逻辑之详细攻略
- 一文详解EventMesh落地华为云的探索及实践
- Summary of "performance test" of special test
- Writing technical articles is a fortune for the future
- 个人如何投资理财比较安全?
猜你喜欢

Esp8266 system environment setup

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

【C语言进阶】指针的进阶【中篇】
叮咚抢菜-派送时段监听及推送工具

Summary of "performance test" of special test

Looking for a room in the graduation season of college students, VR panoramic viewing helps you screen Online

Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)

Domain Driven Design (VI) - Architecture Design

How to increase the monthly salary of software testing from 10K to 30K? Only automated testing can do it

Yuntu says that every successful business system cannot be separated from apig
随机推荐
HW蓝队中级面试复盘
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
Analyse du code source de Tencent libco CO CO - Process open source library
Esp8266 system environment setup
叮咚抢菜-派送时段监听及推送工具
Design and development of hospital reservation registration platform based on JSP Zip (thesis + project source code)
【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】
Morris traversal of binary tree
马斯克称自己不喜欢做CEO,更想做技术和设计;吴恩达的《机器学习》课程即将关闭注册|极客头条
长颈鹿的脖子变长其实不是因为吃高处的树叶?科学家在1700万年前的化石中找到答案
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
SAR image focusing quality evaluation plug-in
腾讯Libco协程开源库 源码分析 全系列总结博客
2022.05.26 (lc_1143_longest common subsequence)
ESP8266 系统环境搭建
Logback exclude specified package / class / method log output
An error row size too large (& gt; 8126) occurs when MySQL's MyISAM engine switches to InnoDB
Summary of "performance test" of special test
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
Source code analysis and practical testing openfeign load balancing