0

0

交换每两个字节中的每两个位

WBOY

WBOY

发布时间:2023-09-11 23:01:02

|

1544人浏览过

|

来源于tutorialspoint

转载

交换每两个字节中的每两个位

在本文中,我们将讨论交换给定数字中的每个交替位的代码解决方案,并返回结果数字。我们将使用位操作的概念来解决这个问题,以在不使用任何循环的情况下以恒定时间解决问题。

Problem statement − We are given a number n, we have to swap the pair of bits that are adjacent to each other.

In other words, we have to swap every odd placed bit with its adjacent even placed bit.

Constrain: While solving the problem, we have to keep In mind that we cannot use a loop for this problem, we have to execute our code in O(1) time complexity only.

Example

Input − n = 10011110

输出 - 在交换偶数位置位和奇数位置位之后,

the binary number obtained is: 01101101

Input − n = 10011110

输出 - 在交换偶数位置位和奇数位置位之后,

the binary number obtained is: 01101101

Explanation

让我们考虑前面的例子以更好地理解。

n = 10011110
Even position bits in n are E – 1 x 0 x 1 x 1 x
Odd position bits in n are O – x 0 x 1 x 1 x 0

For the result, we want the even position bits at the odd position and vice-versa

For even position bits at odd position,

We need to right shift the even position by one position.

SocoShop
SocoShop

SocoShop是天易CES开发组利用将近两年的时间,研究了各种商城开发出来的商城系统,开发的语言是net(C#)。无论在功能、操作人性化、运行效率、安全等级和扩展性等方面都居国内外同类产品领先地位。 1、功能强大:SocoShop囊括了当今商城系统的大部分的功能,主要分基础设置、商品管理、用户中心、市场营销、订单与统计五大版块,每个版块又做了很细致的深化,满足不同顾客,不同行业的各种

下载

因此,对于偶数位置的位,我们只需将 E >> 1 来获取所需的位置。

Similarly, we have to left shift the odd position bits by one position to get the desired position of odd bits.

所以,对于奇数位,我们只需要将O

Now the next problem is to extract the odd and even position bits.

正如我们所知,

0x55 = 01010101 in which every only odd position bits are set ( non 0 ).
0xAA = 10101010 in position bits are set. which, only odd

Hence to extract E from n, we just need to perform

E = n & 0xAA

Similarly, to extract O from n, we need to perform-

O = n & 0x55

Now, to find the swapped output,

步骤

涉及的步骤为-

  • E >> 1

  • O

  • Now, we combine E and O using or operation.

  • Hence our result will be – Result = ( E >> 1 | O

Example

的中文翻译为:

示例

这种方法的代码表示如下:

#include
using namespace std;
unsigned int swapbits(unsigned int n) {
   unsigned int E = n & 0xAA ;
   unsigned int O = n & 0x55 ;
   unsigned int result = (E >> 1)|(O << 1);
   return result;
}
int main() {
   unsigned int n = 14;
   cout << "After swapping the even position bits with off position bits, the binary number obtained is " << swapbits(n) << endl;
   return 0;
   // code is contributed by Vaishnavi tripathi
}

Output

After swapping the even position bits with off position bits, the binary number obtained is 13

时间复杂度 - 这种方法的时间复杂度为O(1)。

空间复杂度 - 我们没有使用任何额外的空间。辅助空间复杂度为O(1)。

相关专题

更多
while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

88

2023.09.25

CSS position定位有几种方式
CSS position定位有几种方式

有4种,分别是静态定位、相对定位、绝对定位和固定定位。更多关于CSS position定位有几种方式的内容,可以访问下面的文章。

81

2023.11.23

点击input框没有光标怎么办
点击input框没有光标怎么办

点击input框没有光标的解决办法:1、确认输入框焦点;2、清除浏览器缓存;3、更新浏览器;4、使用JavaScript;5、检查硬件设备;6、检查输入框属性;7、调试JavaScript代码;8、检查页面其他元素;9、考虑浏览器兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

181

2023.11.24

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

0

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

32

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

14

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.3万人学习

Django 教程
Django 教程

共28课时 | 3.1万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号