当前位置:网站首页>Blue Bridge Cup Eliminate last one (bit operation, code completion)
Blue Bridge Cup Eliminate last one (bit operation, code completion)
2022-07-02 21:41:00 【Woodenman Du】
requirement :
The following code represents an integer binary representation of the rightmost consecutive 1 All become 0 If the last one is 0, Then the original number remains unchanged .
Need to complete the code
#include <stdio.h>
void f(int x)
{
int i;
for(i=0; i<32; i++) printf("%d", (x>>(31-i))&1);
printf("....");
x = _______________________;
for(i=0; i<32; i++) printf("%d", (x>>(31-i))&1);
printf("\n");
}
int main()
{
f(128+64+2);
f(128+64+15);
f(128+64+1);
return 0;
}
answer :x&(x+1)
analysis :
First of all, clarify what to achieve in space , Observe the empty front and back x Binary conversion of , That is to say, the empty line should realize the end 1 The transformation of
Now analyze how to get rid of 1, We should put the continuous tail 1 All become 0, First of all, we have to create 0 Just come , Because of a pile of 11111 Add a 1 You'll get equal 0, So there must be an answer x+1
then x+1 Followed by a continuous tail 1 All become 0, The rest , Continuous tail 1 The higher position of the becomes 1, The rest has not changed
take x And x+1 By comparison, we can find , Every one of you , It is both Same as 1 by 1, Same as 0 by 0, Different for 0, You can get the destination binary , Think of biting and , The answer comes out
eg:
x: 101010111
x+1: 101011000
The goal is :101010000
边栏推荐
- Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
- [shutter] shutter layout component (physicalmodel component)
- [dynamic planning] p1220: interval DP: turn off the street lights
- Sword finger offer (I) -- handwriting singleton mode
- China plastic bottle and container market trend report, technological innovation and market forecast
- Gbase8s database type
- Micro SD Card Industry Research Report - market status analysis and development prospect forecast
- MySQL inserts Chinese data and reports an error. Set the default collation
- China's log saw blade market trend report, technological innovation and market forecast
- treevalue——Master Nested Data Like Tensor
猜你喜欢
基本IO接口技术——微机第七章笔记
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
rwctf2022_ QLaaS
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
如何防止你的 jar 被反编译?
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
26 FPS video super-resolution model DAP! Output 720p Video Online
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
MySQL learning record (6)
[hands on deep learning]02 softmax regression
随机推荐
The neo4j skill tree was officially released to help you easily master the neo4j map database
Codeworks global round 19 (CF 1637) a ~ e problem solution
It is said that this year gold three silver four has become gold one silver two..
Cloud computing technology [1]
rwctf2022_ QLaaS
MySQL learning record (4)
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
beginning
Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
Research Report on market supply and demand and strategy of Chinese garden equipment industry
Construction and maintenance of business website [1]
Basic knowledge of tree and binary tree (detailed illustration)
Three chess games
Research Report on micro gripper industry - market status analysis and development prospect prediction
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
Sword finger offer (II) -- search in two-dimensional array
Accounting regulations and professional ethics [18]
暑期第一周总结