当前位置:网站首页>[C] Inverts the binary of a decimal number and outputs it
[C] Inverts the binary of a decimal number and outputs it
2022-06-12 23:57:00 【PowerDon】
describe
Here are three ways ( The third kind of accidental thought , Ha ha ha , Very simple )
The first way
This solution is more conventional , The idea is to initialize an array ( The default value is 0),
Then take each one out , Replace the corresponding value in the array , Because when you take binary
It is taken from right to left , And the array is stored from left to right , So the number you get
A group is a sequence in reverse order .
void reverse_bits(unsigned int value)
{
int result_buffer[32] = { 0 };
int* Tag = result_buffer;
while (value != 0)
{
int temp = value / 2;
*Tag++ = value - temp * 2;
value /= 2;
}
int i;
for (i = 0; i < 32; i++)
{
printf("%d", result_buffer[i]);
}
}The second way
This is an improved version of the previous method , In a unique way ,
The binary number is used to verify the original number ( namely 2 To the power of , This is used for verification
In binary form, only one bit is 1), Make this decimal by logical and
Count it down , And stored in the array .
void reverse_bits(unsigned int value)
{
int result_buffer[32] = { 0 };
int i;
for (i = 0; i < 32; i++)
{
int temp = (value & (int)pow(2,i)) != 0 ? 1 : 0;
result_buffer[i] = temp;
}
for (i = 0; i < 32; i++)
{
printf("%d", result_buffer[i]);
}
}The third way
This method is based on the second method , Since we can
Find each bit , So why can't we just put together this number in binary ?
Simply move left , Relative to the limitations of getting an array , A concrete
The values are obviously more flexible , And more in line with the meaning of the topic .
unsigned int reverse_bits(unsigned int value)
{
unsigned int result = 0;
int i;
for (i = 0; i < 32; i++)
{
result += (((value & (int)pow(2, i)) != 0 ? 1 : 0) << (31-i));
}
printf("%u", result);
return result;
}
边栏推荐
- SAP 业务技术平台(BTP) Workflow(工作流)功能介绍
- [matlab] basic operation
- 机加工行业MES系统模具行业MES系统CNCl中工行业MES系统MES扫码报工MES数据采集
- Case sharing of online real queuing system reconfiguration -- practical part
- 如何快速查询手机号码归属地和运营商
- Leetcode 2164. Sort odd and even subscripts separately (yes, once)
- Design MySQL table structure for message queue to store information data
- 2022美容师(技师)上岗证题目及答案
- 2022 operation of simulated examination platform for hoisting machinery command certificate
- 2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試
猜你喜欢

Will PM (Project Manager) take the PMP Exam?

如何实现OSM地图本地发布并自定义配图
![[matlab] matrix](/img/15/da3b530fca50080392c89e91998526.png)
[matlab] matrix

如何让矢量瓦片配图神器maputnik支持 geoserver
![[hcie discussion] STP-A](/img/4f/7a76555fc7b23eafa108699f5aa5ad.png)
[hcie discussion] STP-A

KAUST:Deyao Zhu | 价值记忆图:基于离线强化学习的图结构世界模型

Based on three JS offshore wind power digital twin 3D effect

基于Three.js海上风电数字孪生三维效果

2022年G3锅炉水处理考题模拟考试平台操作

Kaust:deyao Zhu | value memory map: a graph structured world model based on off-line reinforcement learning
随机推荐
[literature translation - Part] revealing the structure of clinical EEG signals by self supervised learning (SSL and RP principles / data / preprocessing)
2022年6月11日记:王老师的春天,混入
[matlab] matrix
How SAP ui5 uses manifest JSON file defines third-party library dependencies
【HCIE论述】STP-A
PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
Tsinghua University image source will cause tensorflow GPU installation failure
Summary of individual NLP internship experience
Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
vs studio_ How to use scanf in 2022
PMP test difficulty and pass rate
OSM map local publishing - how to generate vector maps of provinces and cities
H5时代leaflet中还在用DivIcon?
在 Golang 中构建 CRUD 应用程序
KAUST:Deyao Zhu | 价值记忆图:基于离线强化学习的图结构世界模型
June 11, 2022 diary: Mr. Wang's spring, mixed in
SAP 业务技术平台(BTP) Workflow(工作流)功能介绍
KConfig
Video tracker error troubleshooting
Tableau
