[CC150] Chapter 5 Bits Manipulation
5.1 You are given two 32-bit number, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because M could not fully fit between bit 3 and bit 2.
EXAMPLE
Input: N = 10000000000, M = 10011, i = 2, j = 6
Output: N = 10001001100
Using bitset is trivial...
Better solution should be use a mask. According to the solution, the problem can be divided into three steps: 1) clear the bits j through i in N; 2) Shift M so that it lines up with N; 3) Merge M and N.
5.2 Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".
Note that in the following code, I did not print "ERROR" for more than 32 bits.
5.3 Given a positive integer, print the next smallest and the next largest number that have the same number of 1 bits in their binary representation.
TBF.
5.4 Explain what the following code does: ((n & (n - 1)) == 0).
Suppose n = abcde1000 in binary. n - 1 is then abcde0111. n & (n - 1) is abcde0000. Hence, in order to make sure that n & (n -1) == 0, abcde have to be all zero. In other words, n has to be a power of 2 or 0.
5.5 Write a function to determine the number of bits required to convert integer A to integer B.
EXAMPLE
Input: 31 = 11111, 14 = 01110
Output: 2
It is equivalent to count the number of 1s in A ^ B. One way is to use bit shift; another tricky approach is to continuously flip the least significant bit ( c = c & (c - 1) ).
5.6 Write a program to swap odd and even bits in an integer with as few instructions as possible.
TBF.
5.7 An array A 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 bits of A[i]," which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?
TBF.
5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is divisible by 8 (that is, no byte will be split across rows). The height of the screen, of course, can be derived from the length of the array and the width. Implement a function which draws a horizontal line from (x1, y) to (x2, y).
TBF.
EXAMPLE
Input: N = 10000000000, M = 10011, i = 2, j = 6
Output: N = 10001001100
Using bitset is trivial...
Better solution should be use a mask. According to the solution, the problem can be divided into three steps: 1) clear the bits j through i in N; 2) Shift M so that it lines up with N; 3) Merge M and N.
5.2 Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".
Note that in the following code, I did not print "ERROR" for more than 32 bits.
5.3 Given a positive integer, print the next smallest and the next largest number that have the same number of 1 bits in their binary representation.
TBF.
5.4 Explain what the following code does: ((n & (n - 1)) == 0).
Suppose n = abcde1000 in binary. n - 1 is then abcde0111. n & (n - 1) is abcde0000. Hence, in order to make sure that n & (n -1) == 0, abcde have to be all zero. In other words, n has to be a power of 2 or 0.
5.5 Write a function to determine the number of bits required to convert integer A to integer B.
EXAMPLE
Input: 31 = 11111, 14 = 01110
Output: 2
It is equivalent to count the number of 1s in A ^ B. One way is to use bit shift; another tricky approach is to continuously flip the least significant bit ( c = c & (c - 1) ).
5.6 Write a program to swap odd and even bits in an integer with as few instructions as possible.
TBF.
5.7 An array A 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 bits of A[i]," which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?
TBF.
5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to be stored in one byte. The screen has width w, where w is divisible by 8 (that is, no byte will be split across rows). The height of the screen, of course, can be derived from the length of the array and the width. Implement a function which draws a horizontal line from (x1, y) to (x2, y).
TBF.
Comments
Post a Comment