当前位置:网站首页>Interview question 05.07 Pairing exchange
Interview question 05.07 Pairing exchange
2022-07-22 13:29:00 【Mr Gao】
Interview questions 05.07. Pairing exchange
Pairing exchange . Programming , Exchange the odd and even digits of an integer , Use as few instructions as possible ( in other words , position 0 And bit 1 In exchange for , position 2 And bit 3 In exchange for , And so on ).
Example 1:
Input :num = 2( perhaps 0b10)
Output 1 ( perhaps 0b01)
Example 2:
Input :num = 3
Output :3
This question is actually relatively simple , We need to master some knowledge of bit operation :
The solution code is as follows , We can keep even bits and odd bits respectively, then move left and right, and then add
int exchangeBits(int num)
{
int a = 0B10101010101010101010101010101010;
int b = 0B01010101010101010101010101010101;
a &= num; // Keep odd digits
b &= num; // Keep even digits
a >>= 1; // Swap parity bits
b <<= 1;
int ans = a | b;// The odd and even bits can be combined together
return ans;
}
边栏推荐
- 2022/07/20---把字符串转换成整数; 滑动窗口的最大值
- Elemen when clicking, modify the playback index of the walking lantern
- 人体体重秤体脂秤方案PCBA设计
- Typescript - quick start
- 2022年数据库审计产品排行榜-必看!
- Qt 使用 Google Breakpad 捕获程序崩溃报告(dump文件)
- 如何将沥青高位槽液位数值无线传输至载热体记录仪监测?
- One bite of Stream(8)
- 如何正确使用call、bind、apply
- The difference between join on followed by condition and where followed by condition
猜你喜欢
Qt 使用 Google Breakpad 捕获程序崩溃报告(dump文件)
Qt Creator快捷键大全,附快捷键配置方法
ideal关于 log显示问号且双击无法打不开的解决方法
利用浏览器插件运行js来删除特定网站“禁用copy”功能
Elemen when clicking, modify the playback index of the walking lantern
uniapp 实现抽奖幸运大转盘功能
Coordinate system in QT
Qt5中创建及使用自定义插件遇到的一些问题
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
谈谈实施数据治理时常犯的10大错误
随机推荐
elemen点击时,修改走马灯的播放索引
谈谈实施数据治理时常犯的10大错误
Go list 修改元素值
2022/07/21---滑动窗口的最大值;
Elephant Swap的LaaS方案优势分析,致eToken表现强势
C. Doremy's IQ (greedy)
【HCIP题库哪里买?】
Privacy-Preserving Generative Deep Neural Networks Support Clinical Data Sharing
Addition, deletion, query and modification of [mdsql] table (Advanced)
(ICLR-2021)一幅图像相当于16X16个words:大规模图像识别的Transformer
uniapp 实现抽奖幸运大转盘功能
ideal关于 log显示问号且双击无法打不开的解决方法
麒麟系统 QT代码 出现 使用未声明的标识符“MainWindow“
leetcode 792. Number of matching subsequences
Recordrtc video recording, playback, screenshot, Download
QT program packaging and publishing method (using the official windeployqt tool)
service(lb) 和管理的pod
如何安装mysql
(iclr-2021) an image is equivalent to 16x16 words: a transformer for large-scale image recognition
Use the browser plug-in to run JS to delete the "disable copy" function of a specific website