当前位置:网站首页>[code hoof set novice village question 600] how to change the binary value at the even position of all binaries of a decimal integer to 0
[code hoof set novice village question 600] how to change the binary value at the even position of all binaries of a decimal integer to 0
2022-07-21 05:50:00 【Sandm *】
Title Description :
Their thinking :
Because it's processing on binary bits ,
So we need to use binary operators ,
Such as :
& ^ | << >>
First create a variable to record binary digits ,
By shift right operator >> Gradually advance from right to left to every bit of binary ,
And make its even digit become 0 Methods :
By subtracting 1 * 10 Of n Power ,( Binary system ), Which is equivalent to subtracting decimal 2^n
among n The value is selected according to the binary position :
for example :
Subtract binary 10 The last digit remains unchanged
![]()
Subtract binary 100 after , The last digit remains unchanged
Remember to ,
The number of bits of the final binary number is constant ,
So only by subtracting 1 * 10 In order to ensure that the last digit remains unchanged
【 matters needing attention 】:
- This class deals with problems in binary bits ,
The easiest mistake to make :
It ignores that the processing and storage of data by the computer itself are carried out in binary bits
Just print in the final output ,
That is, when human-computer interaction ,
You need to use the format controller or stream ,
Print out binary numbers in decimal format
therefore ,
There is no need to design an algorithm to convert the input decimal data into binary
- Shift right operator >> Equivalent to divided by 2
- Shift left operator << Equivalent to times 2
Reference code :
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int data = 0;
cin >> data;
int tmp = data;
int id = 0;// Used to indicate its subscript
int sub = 1;// Must be initialized to 1
while(tmp)
{
if((tmp % 2 == 1) && (id % 2 == 0))
/* Judge whether the binary bits at this time are even digits , Whether the value on the even digit is 1*/
{
data = data - sub;
}
sub = sub * 2;
id++;
tmp = tmp >> 1;// The shift right operator is equivalent to dividing by 2
// Or is it tmp = tmp / 2;
}
cout << data;
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Usage of mappartitions in pyspark
怎么才能写好技术文档——这是我的全部经验
PicGo配置阿里云oss
牛客-TOP101-BM33
Construction scheme of smart mine digital twin management system
「接口测试入门课」打卡学习 day07:WebSocket接口:如何测试一个完全陌生的协议接口?
「接口测试入门课」打卡学习 day08:测试数据,是不是可以把所有的参数都保存到Excel中?
发电机组工作安排问题代码
刚入门软件测试行业的女生就能月薪过万骗局解秘
ByteDance (Tiktok) software test monthly salary 23K post, technical director three interview questions are newly released
智慧矿山数字孪生管理系统建设方案
Online education project integration swagger
[C language brush leetcode] 731 My schedule II (m)
【码蹄集新手村 600 题】如何将一个十进制整数的所有二进制的偶数位置上的二进制数值都变为 0
[MySQL] current date minus one day field year month day -1 day
Spss-kmeans聚类实操
(第二章一一栈与队列)第二节 栈的链式存储
大佬们,请问 PostgreSQL CDC ,PostgreSQL 必须配置逻辑复制吗? 目前部署的
浅谈Calcite的Calc优化
发电机组工作安排问题