(25 points) Problems:
(2.43/2.61) – Using only bit-level and logic operations, write C expressions that yield 1 for the described condition, and 0 otherwise. Assume that x is of type int. You may not use any equality (==) or inequality (!=) tests.
- Any bit of x equals 1. _______________________________________________
- Any bit of x equals 0. _______________________________________________
- Any bit in the least significant byte of x equals 1. _________________________
- Any bit in the least significant byte of x equals 0. _________________________
Hint: One solution to part (a.) is: !!x (and, yes, you can use this as your solution :-).
- (2.81) – Write C expressions to generate the bit patterns that follow, where ak represents k repetitions of symbol a. Assume a w-bit data type. Your code may contain references to j and k, representing the values of j and k, but not a parameter representing w.
- 1w-k 0k
- 0w-k-j 1k 0 j
For example, if we are dealing with chars, an 8-bit data type, and k = 3, then for a. we want an expression that results in 11111000.
- (2.50/2.76/2.77) – Suppose we are given the task of generating code to multiply integer variable x by various different constant factors K. To be efficient, we want to use only the operations +, -, and <<. For the following values of K, write C expressions to perform the multiplication using at most three operations per expression.
- K = 31: ________________________________________________________
- K = -7: ________________________________________________________
- K = 80: ________________________________________________________
- K = 144: ________________________________________________________
Hint: One solution to part (a.) is: (x<<5) – x.
- (2.55/2.82/2.83) – Consider the numbers having a binary representation consisting of an infinite string of the form 0.yyyyyy…, where y is a k-bit sequence. For example, the binary representation of 1/3 is 0.0101010101… (that is, y = 01, and k = 2), while the representation of 1/5 is 0.001100110011… (that is, y = 0011, and k = 4).
- Give a formula in terms of y and k for the value represented by the infinite string. Hint: Consider the effect of shifting the binary point k positions to the right, and do part b. first…
- What is the numeric value of the string for the following values of y? Note that the value of k is implied; e.g., for case i., k = 3, etc.
- 110 ____________________________________________________ ii. 01001 ____________________________________________________ iii. 010111 ____________________________________________________
- Consider a 16-bit two’s complement representation for signed integers. Fill in the empty boxes in the following table. Spaces in the binary representation are just added to enhance readability.
|Number||Decimal Representation||Binary Representation|
|0000 0000 0000 0101|
|TMin||1000 0000 0000 0000|
|TMin + TMin|
|1111 1111 1110 0111|
Show work. Hint: 215 = 32768, 214 = 16384, .. , 25 = 32, 24 = 16, .. , 21 = 2, 20 = 1