当前位置:网站首页>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
边栏推荐
- Unexpectedly, there are such sand sculpture code comments! I laughed
- Cloud computing technology [1]
- Interpretation of some papers published by Tencent multimedia laboratory in 2021
- China's crude oil heater market trend report, technological innovation and market forecast
- Analysis of enterprise financial statements [4]
- Construction and maintenance of business websites [6]
- Detailed explanation of OSI seven layer model
- Import a large amount of data to redis in shell mode
- Browser - clean up the cache of JS in the page
- MySQL installation failed -gpg verification failed
猜你喜欢

7. Build native development environment
![[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)](/img/17/b5889ec263687aeacf19214785ea8a.jpg)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)

关于测试用例

【零基础一】Navicat下载链接

Etcd Raft 协议
![[hands on deep learning]02 softmax regression](/img/47/eb67ec2c51f6bb7d6b2879b36e769d.jpg)
[hands on deep learning]02 softmax regression

Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring

Welfare, let me introduce you to someone

5 environment construction spark on yarn
![The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article](/img/8f/6759b4685a129f9d10d6ea1dc8e61e.jpg)
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
随机推荐
[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Redis -- three special data types
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
Micro SD Card Industry Research Report - market status analysis and development prospect forecast
如何防止你的 jar 被反编译?
China's log saw blade market trend report, technological innovation and market forecast
[CV] Wu Enda machine learning course notes | Chapter 12
Who do you want to open a stock account? Is it safe to open a mobile account?
kernel_ uaf
Analysis of enterprise financial statements [3]
Unexpectedly, there are such sand sculpture code comments! I laughed
Analyze comp-206 advanced UNIX utils
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
Interpretation of some papers published by Tencent multimedia laboratory in 2021
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
Chinese Indian seasoning market trend report, technical dynamic innovation and market forecast
Sword finger offer (II) -- search in two-dimensional array
MySQL learning record (4)