Question

In: Computer Science

Using C (not C++): setFirst - returns value with n upper bits set to 1 and...

Using C (not C++):

setFirst - returns value with n upper bits set to 1 and 32-n lower bits set to 0

* You may assume 0 <= n <= 32

* Example: setFirst(4) = 0xF0000000

* Legal ops: ! ~ & ^ | + << >> (NO IF OR FOR LOOPS)

* Max ops: 10

* Rating: 2

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <stdio.h>

unsigned int setFirst(int n)
{
// take the number as 0.
unsigned int num=0;
  
// left shift the number of bits n and subtract 1 from the result.
num = (1 << (n)) - 1;
  
// shift the bits to right and return the answer
return num<<(32-n);
}
int main()
{
printf("n=4. It's Hex representation is %x\n",setFirst(4));
printf("n=8. It's Hex representation is %x\n",setFirst(8));
printf("n=15. It's Hex representation is %x\n",setFirst(15));

return 0;
}

==========


Related Solutions

Q8. If the bits 0 and 2 of P1IN are set, then toggle all the bits...
Q8. If the bits 0 and 2 of P1IN are set, then toggle all the bits of P1OUT simultaneously except the bit 4, else toggle only the bit 4 of P1OUT.
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Determine the upper-tail critical value t(a/2) in each of the following a. 1-a = 0.95, n=50...
Determine the upper-tail critical value t(a/2) in each of the following a. 1-a = 0.95, n=50 b. 1-a = 0.99, n=50 c. 1-a = 0.95, n=63 d. 1-a = 0.95, n=35 e. 1-a = 0.90, n=56
in the MSP430FR6989 what bits are set to “turn on” LCD_B1?  
in the MSP430FR6989 what bits are set to “turn on” LCD_B1?  
Test the hypothesis using the​ P-value approach. Upper H 0 : p equals 0.50 versus Upper...
Test the hypothesis using the​ P-value approach. Upper H 0 : p equals 0.50 versus Upper H 1 : p less than 0.50 n equals 150 comma x equals 66 comma alpha equals 0.10 Perform the test using the​ P-value approach. ​P-valueequals nothing ​(Round to four decimal places as​ needed.)
[02] For n ≥ 1, how many strings of length n using letters a,b,c are there...
[02] For n ≥ 1, how many strings of length n using letters a,b,c are there if the letter a must occur an even number of times?
Add 011 0011 to 010 1100 and show the V and C bits (seven bits and...
Add 011 0011 to 010 1100 and show the V and C bits (seven bits and show work)
Add 111 0011 to 100 1100 and show the V and C bits (seven bits)
Add 111 0011 to 100 1100 and show the V and C bits (seven bits)
7. Let n ∈ N with n > 1 and let P be the set of...
7. Let n ∈ N with n > 1 and let P be the set of polynomials with coefficients in R. (a) We define a relation, T, on P as follows: Let f, g ∈ P. Then we say f T g if f −g = c for some c ∈ R. Show that T is an equivalence relation on P. (b) Let R be the set of equivalence classes of P and let F : R → P be...
Using Theorem 1, discuss lim n→∞ fn on B and C (as in Example (a)) for...
Using Theorem 1, discuss lim n→∞ fn on B and C (as in Example (a)) for each of the following. (i) fn(x) = x n ; B = E1; C = [a, b] ⊂ E1. (ii) fn(x) = cos x + nx n ; B = E1. (iii) fn(x) = Xn k=1 xk; B = (−1, 1); C = [−a, a], |a| < 1. (iv) fn(x) = x 1 + nx ; C = [0, +∞). [Hint: Prove that Qn...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT