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相关的文章、下载、课程内容,供大家免费下载体验。

81

2023.09.25

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

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

80

2023.11.23

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

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

180

2023.11.24

excel制作动态图表教程
excel制作动态图表教程

本专题整合了excel制作动态图表相关教程,阅读专题下面的文章了解更多详细教程。

24

2025.12.29

freeok看剧入口合集
freeok看剧入口合集

本专题整合了freeok看剧入口网址,阅读下面的文章了解更多网址。

74

2025.12.29

俄罗斯搜索引擎Yandex最新官方入口网址
俄罗斯搜索引擎Yandex最新官方入口网址

Yandex官方入口网址是https://yandex.com;用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

207

2025.12.29

python中def的用法大全
python中def的用法大全

def关键字用于在Python中定义函数。其基本语法包括函数名、参数列表、文档字符串和返回值。使用def可以定义无参数、单参数、多参数、默认参数和可变参数的函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

python改成中文版教程大全
python改成中文版教程大全

Python界面可通过以下方法改为中文版:修改系统语言环境:更改系统语言为“中文(简体)”。使用 IDE 修改:在 PyCharm 等 IDE 中更改语言设置为“中文”。使用 IDLE 修改:在 IDLE 中修改语言为“Chinese”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

18

2025.12.29

C++的Top K问题怎么解决
C++的Top K问题怎么解决

TopK问题可通过优先队列、partial_sort和nth_element解决:优先队列维护大小为K的堆,适合流式数据;partial_sort对前K个元素排序,适用于需有序结果且K较小的场景;nth_element基于快速选择,平均时间复杂度O(n),效率最高但不保证前K内部有序。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

12

2025.12.29

热门下载

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

精品课程

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

共48课时 | 6.3万人学习

Django 教程
Django 教程

共28课时 | 2.6万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

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

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