//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, etc).
#include <iostream>
#include <vector>
using namespace std;
int getNum1(int N)
{
int num=0;
for (int i=0; i<32; i++)
{
int t = N&1;
if (t == 1)
{
num ++;
}
N = N>>1;
}
return num;
} void print(int p)
{
vector<int> v;
for (int k = 0;k<32; k++)
{
int t = p&1;
v.push_back(t);
p=p>>1;
}
for (int i = v.size()-1;i>-1; i--)
{
cout<<v[i]<<" ";
}
cout<<endl;
} int getDiff(int a, int b)
{
return getNum1(a^b);
}
int swap(int a)
{
int aa = a;
int j = aa & 0x55555555;
int o = ((aa>>1) & 0x55555555);
return (j<<1)|(o);
}
int main()
{
/*int a = 8;
int b = 6;
print(a);
print(b);*/
//cout<<getDiff(a,b)<<endl;
int k = 111;
print(k);
print(swap(k));
return 0;
}

Cracking The Coding Interview 5.6的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. google搜索小技巧

    google搜索小技巧 一.总结 一句话总结:But most people may not be using Google search to its full potential.Want to ...

  2. Hisat2 bowtie2比对结果解读(Hisat2 Alignment summary)

    RNA-seq数据的比对结果怎么解读?网上有很多人问,这里做一个大致的总结. Hisat2和bowtie2比对后产生的Alignment summary的格式是一样的,如下: Alignment su ...

  3. OpenVPN参数详解

    一般选项: –config file : 从file中读取配置选项. –help : 显示选项. –version : 显示版权和版本信息. 隧道选项: –local host : 本地主机名或IP地 ...

  4. c# winform 中的坐标系

    从数学角度讲,Point是一个二维矢量,包含两个公共整型属性,属性用大写X和Y(c#中公共属性一般约定以大写字母开头).当坐标不是整数值是float时,用PointF代替Point使用. 常用的Siz ...

  5. 三种css样式表及其优先级

    1.行内样式 body内: <p style="text-indent: 2em;color: red"> 我是行内样式 </p> 2.内部样式表 body ...

  6. mysql常用修改创建语句

    一.连接数据库 <?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = ...

  7. 2017-2018 ACM-ICPC Latin American Regional Programming Contest D.Daunting device

    题意:一个数组n个操作每次先查询p颜色的数量然后求出区间,区间染色成x,然后求最大染色数 题解:odt裸题,多维护一个color个数数组就好了 //#pragma comment(linker, &q ...

  8. 解决jar包依赖:Spring IO platform推出bom

    名词解释: BOM(bill of materials):材料清单,用于解决jar包依赖的好方法. Spring IO Platform 缘起:Spring起初只专注ioc和aop,现在已发展成一个庞 ...

  9. 十、 持久层框架(MyBatis)

    一.基于MyBatis动态SQL语句 1.if标签 实体类Product的字段比较多的时候,为了应付各个字段的查询,那么就需要写多条SQL语句,这样就变得难以维护. 此时,就可以使用MyBatis动态 ...

  10. npm使用国内淘宝镜像的方法

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...