Cracking the coding interview--Q5
来源:互联网 发布:天谕豪男捏脸数据 编辑:程序博客网 时间:2024/06/10 14:54
Chapter 5 | Bit Manipulation
5.1 You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j). EXAMPLE: Input: N = 10000000000, M = 10101, i = 2, j = 6 Output: N = 10001010100
先清零再或?
5.2 Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR”
不会。
5.3 Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation.
穷举
5.4 Explain what the following code does: ((n & (n-1)) == 0).
判断是否为2的整数次幂。
5.5 Write a function to determine the number of bits required to convert integer A to integer B. Input: 31, 14 Output: 2
傻瓜方法就是一个一个比,好一点就是异或?
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, etc).
原解挺妙。0x5555和移位或来配合。
5.7 An array A[1…n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is “fetch the jth bit of A[i]”, which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?
原解感觉略弱,或许是我没有理解清楚题意。
- Cracking the coding interview--Q5
- Cracking the coding interview--Q5.1
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q5.3
- Cracking the coding interview--Q5.4
- Cracking the coding interview--Q5.5
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q5.7
- Cracking the coding interview--Q5.1
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q5.3
- Cracking the coding interview--Q5.5
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q5.7
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- andengine-3:用手画线并且sprite随画的线条移动
- poj 1679 The Unique MST
- 《C Plus Primer》第七章编程题
- 学习C++ Windows 编程(你的第一个Windows程序)
- MapObjects shape数据操作 二
- Cracking the coding interview--Q5
- openstack 源码分析之swift proxy 服务启动 2
- Ubuntu 12.04 如何修改背景色
- java基础一些简易算法题
- android 重新打包
- poj 2417 离散对数模方程+哈希
- PHP环境搭建:Windows 7下安装配置PHP+Apache+Mysql环境教程
- 转:谷歌内幕:详解互联网巨头的做事逻辑
- Ant编译android程序