[CareerCup] 5.6 Swap Odd and Even Bits 交换奇偶位
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 交换奇偶位的更多相关文章
- [CareerCup] 17.1 Swap Number In Place 互换位置
17.1 Write a function to swap a number in place (that is, without temporary variables). 这道题让我们交换两个数, ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- C. Swap Letters 01字符串最少交换几次相等
C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...
- LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- 关于在Xcode的OC工程中相对路径失败的原因
Xcode的工程生成的可执行文件不是默认在源文件同一个目录下面的,所以当可执行文件执行的时候,相对路径就不对了. 这一点用终端直接编译执行文件证明了这一点: clang -fobjc-arc -fra ...
- 【AdaBoost算法】强分类器训练过程
一.强分类器训练过程 算法原理如下(参考自VIOLA P, JONES M. Robust real time object detection[A] . 8th IEEE International ...
- Effective Java 47 Know and use the libraries
Advantages of use the libraries By using a standard library, you take advantage of the knowledge of ...
- 读书摘要:第七章 闩Suan锁和自旋锁
摘要: 1.闩锁就像是内存上的锁,随着越来越多的线程参与进来,他们争相访问同一块内存,导致堵塞.2.自旋锁就是闩锁,不同之处是如果访问的内存不可用,它将继续检查轮询一段时间.3.拴锁和自旋锁是我们无法 ...
- spring mvc 配置文件拦截器过滤url
最近在用spring mvc拦截器,sprin 版本号4.0.6.RELEASE, <mvc:interceptor> <mvc:mapping path="/admin/ ...
- APACHE重写去除入口文件index.php
下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index.php/index 改变后的路径是: localhost/ind ...
- Linux awk
一.简介 二.教程 1)过滤字符(对大小写很敏感) dir -l | awk '$3=="root" {print $1,$3,$4, $9;} ' cat tecmint_dea ...
- Linux command’s Array
#数组的声明与遍历 animals=("a dog" "a cat" "a fish") #wrong ways to use this f ...
- Stanford机器学习笔记-1.线性回归
Content: 1. Linear Regression 1.1 Linear Regression with one variable 1.1.1 Gradient descent algorit ...
- 三维网格形变算法(Laplacian-Based Deformation)
网格上顶点的Laplace坐标(均匀权重)定义为:,其中di为顶点vi的1环邻域顶点数. 网格Laplace坐标可以用矩阵形式表示:△=LV,其中,那么根据网格的Laplace坐标通过求解稀疏线性方程 ...