当前位置:网站首页>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
边栏推荐
- Sword finger offer (II) -- search in two-dimensional array
- Record the problems encountered by nodejs asynchronism
- Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
- Go language learning summary (5) -- Summary of go learning notes
- The neo4j skill tree was officially released to help you easily master the neo4j map database
- [shutter] statefulwidget component (pageview component)
- Physical layer cables and equipment
- When Valentine's Day falls on Monday
- Basic knowledge of tree and binary tree (detailed illustration)
- Research Report on right-hand front door industry - market status analysis and development prospect forecast
猜你喜欢

Common routines of compressed packets in CTF

Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027

rwctf2022_ QLaaS

MySQL learning record (8)

Capacity expansion mechanism of ArrayList
![[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)](/img/a7/0b87fa45ef2edd6fac519b40adbeae.gif)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)

读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...

MySQL learning record (9)

基本IO接口技术——微机第七章笔记
![[shutter] statefulwidget component (image component | textfield component)](/img/4b/8e54607939989f994303ce5d922331.gif)
[shutter] statefulwidget component (image component | textfield component)
随机推荐
VictoriaMetrics 简介
MySQL installation failed -gpg verification failed
China plastic box market trend report, technological innovation and market forecast
Research Report on market supply and demand and strategy of China's plastic trunking industry
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
MySQL learning notes (Advanced)
Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of microplate instrument industry in China
Research Report on right-hand front door industry - market status analysis and development prospect forecast
Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
Happy Lantern Festival! Tengyuanhu made you a bowl of hot dumplings!
Construction and maintenance of business websites [7]
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
PIP version update timeout - download using domestic image
26 FPS video super-resolution model DAP! Output 720p Video Online
Analysis of enterprise financial statements [1]
Go web programming practice (2) -- process control statement
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
[shutter] statefulwidget component (image component | textfield component)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)