[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 ...
随机推荐
- char与TCHAR相互转化
char与TCHAR相互转化 char strUsr[10] = "Hello"; TCHAR Name[100]; #ifdef UNICODE MultiByteToWideC ...
- informatica 常见问题及解决方案
本文对于informatica使用过程中产生的问题及解决方案做总结,持续更新中... 1.partitioning option license required to run sessions wi ...
- nginx入门(安装,启动,关闭,信号量控制)
公司使用到了nginx,于是周末初步接触了一下nginx,立即被其简洁,优雅,高效的特性给迷住了.nginx是在是个好东西,配置极其简单,容易理解,极其高效,稍微一调优,ab测试10k并发,很轻松.比 ...
- 关于nginx的限速模块
nginx 使用 ngx_http_limit_req_module和ngx_http_limit_conn_module 来限制对资源的请求 这种方法,对于CC攻击(Challenge Collap ...
- Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心
地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...
- CSS3 圆角(border-radius)详解
在做网页的时候,常常需要实现圆角,以前的做法就是切图,现在好了,有了css3的 border-radius 特性之后,实现边框圆角效果就非常简单了,而且其还有多个优点:一是减少网站维护工作量:二是提高 ...
- ZOJ 1109 Language of FatMouse
较简单字典树,每输入一对字符串,前一个放在字典(数组)中,后一个插入字典树中,并将其最终的flag赋为前一个在数组中的下标,再就好找了.输入的处理方法要注意一下. 代码: #include <i ...
- zstu-3769 数回文子串
思路:用manacher求出每个位置的半径,相加即可. 代码:[rad[i]/2]即i这个位置的回文半径,添加的'#'代表长度为偶数的串. #include<stdio.h> #inclu ...
- C# Reflection BindingFlags
定义包含在搜索中的成员 下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.St ...
- SVN代码的回滚二
SVN代码的回滚: 不丢失新建的文件,获得最新的SVN版本控制.TortoiseSVN-ShowLog-选中你要回滚的版本-右键-Export,之后将修改的文件覆盖到你的最新版本,commit即可. ...