当前位置:网站首页>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
边栏推荐
- China plastic box market trend report, technological innovation and market forecast
- Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
- D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
- [C language] [sword finger offer article] - replace spaces
- Analysis of enterprise financial statements [2]
- Go web programming practice (2) -- process control statement
- Who do you want to open a stock account? Is it safe to open a mobile account?
- MySQL inserts Chinese data and reports an error. Set the default collation
- In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028
- [12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
猜你喜欢

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

D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)

Common routines of compressed packets in CTF

The neo4j skill tree was officially released to help you easily master the neo4j map database

Three chess games

System (hierarchical) clustering method and SPSS implementation

How to prevent your jar from being decompiled?

kernel tty_ struct
![[shutter] statefulwidget component (pageview component)](/img/0f/af6edf09fc4f9d757c53c773ce06c8.jpg)
[shutter] statefulwidget component (pageview component)

Welfare, let me introduce you to someone
随机推荐
[shutter] shutter layout component (opacity component | clipprect component | padding component)
~91 rotation
Structure array, pointer and function and application cases
MySQL learning record (1)
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Basic IO interface technology - microcomputer Chapter 7 Notes
Cloud computing technology [1]
China plastic bottle market trend report, technological innovation and market forecast
China plastic box market trend report, technological innovation and market forecast
Basic knowledge of tree and binary tree (detailed illustration)
Import a large amount of data to redis in shell mode
Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
MySQL learning notes (Advanced)
Plastic granule Industry Research Report - market status analysis and development prospect forecast
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
[question brushing diary] classic questions of dynamic planning
Makefile: usage of control functions (error, warning, info)
Analysis of enterprise financial statements [2]
Baidu sued a company called "Ciba screen"
Go language learning summary (5) -- Summary of go learning notes