5.6 Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, and soon).

这道题让我们交换奇偶位,那么我们首先还是要考虑用位操作Bit Manipulation来做,我们知道由于奇偶位是相邻的,奇数位平移一位就是偶数位,反过来偶数位平移一位就是奇数位,那么这题我们可以分别将原来的奇数位和偶数位分别提取出来,各自平移一位,再将其混合成结果即可。提取的方法我们用mask来,对于一个32位的整型数,其奇数位的二进制的mask为10101010101010101010101010101010,换成十六进制数为0xaaaaaaaa,同理偶数位的二进制的mask为01010101010101010101010101010101,换成十六进制数为0x55555555,平移完将其或起来即可,参见代码如下:

class Solution {
public:
int swapOddEvenBits(int x) {
return (((x & 0xaaaaaaaa) >> ) | ((x & 0x55555555) << ));
}
};

[CareerCup] 5.6 Swap Odd and Even Bits 交换奇偶位的更多相关文章

  1. [CareerCup] 17.1 Swap Number In Place 互换位置

    17.1 Write a function to swap a number in place (that is, without temporary variables). 这道题让我们交换两个数, ...

  2. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  3. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  4. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  5. C. Swap Letters 01字符串最少交换几次相等

    C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项

    In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...

  7. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

  8. 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...

  9. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. solrcloud 配置实践

    1.环境 3台虚拟机:192.168.26.129.192.168.26.131.192.168.26.132,使用命令sudo iptables -F 关闭防火墙 Solr: solr-6.1.0 ...

  2. .NET(C#)中不同级别的安全透明代码对类型的影响

    测试代码将测试一个方法和类默认在全部信任权限下和部分信任权限下的代码类型. 上面说的默认就是指未加入其他安全透明类型的特性. 代码类型可以是: 透明代码(Transparent Code) 关键代码( ...

  3. 【转】App开发者必备的运营、原型、UI设计工具整理

    一.运营类 1. APPVIEW,网址:http://lab.hakim.se/appview/ 帮助iOS 应用开发者追踪所有地区App Store最近的用户评论,可以按时间.评分.地区排序,缺点是 ...

  4. shell 脚本关键字&符号

    shell概念 "shell"既是一种解释型编程语言,也是一个这种编程语言的解释器的名字 shell是解释型语言,就是解释器会一条一条的翻译每一条语句并执行,对比之下,C语言是编译 ...

  5. ElasticSearch Filter Aggregations

    类似于sql语句中where子句的作用 { "query": { "match_all": {} }, "aggs": { "ag ...

  6. html跳转倒计时

    <html> <head> <title>出错啦~~~</title> <link href="css/login1.css" ...

  7. 详解apache的allow和deny

    今天看了一篇关于apache allow,deny的文章收获匪浅,防止被删,我直接摘过来了,原文地址!!! !http://www.cnblogs.com/top5/archive/2009/09/2 ...

  8. Zbrush 4R7 P3中各类模型怎么快速隐藏

    在ZBrush®软件中除了遮罩功能可以对模型局部进行编辑外,我们还可以通过显示和隐藏来对模型的局部进行控制. 查看更多内容请直接前往:http://www.zbrushcn.com/jichu/xia ...

  9. leetcode : valid binary search tree

    不能通过 当元素中 有 val == INT_MAX 或者  val == INT_MIN /** * Definition for a binary tree node. * struct Tree ...

  10. 权限框架 - shiro 自定义realm

    上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...