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.
Rotating bits in C This rotates bits to the left which means the bits that were...
Rotating bits in C This rotates bits to the left which means the bits that were shifted off are then moved to the right side. I need B5DE1FAA to become 7787EAAD uiOrig = B5DE1FAA k = 6 unsigned int rotateLeft(unsigned int uiOrig, int k){ }
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.
Have to use C language Pseudocode A #define called BITS should be set at the top...
Have to use C language Pseudocode A #define called BITS should be set at the top of the program. It should be set to 8 when the program is submitted. This define should be used throughout the entire program when setting/using array sizes/max element. This define will also be used in the output to print 8-bit vs 16 bit. Your program should function whether the define is set to 8 or to 16. Part of the grading process will be...
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
Assume that bits is a string that only contains the characters "0" and "1". n is...
Assume that bits is a string that only contains the characters "0" and "1". n is an integer variable that is less than the length of bits. Fill in the blanks below to replace bits with a new string that consists of all of the characters of bits from index n through the end, followed by the first n characters of bits. For example, if bits was "1101010" and n was 3, the new value of bits would be "1010110"....
(1) Show that the set { 1 m + 1 n : m, n ∈ N}...
(1) Show that the set { 1 m + 1 n : m, n ∈ N} is countable. (2) Show that the set {a + b √ 2 : a, b ∈ Q} is countable. (3) Show that the intersection of two countable sets is countable. (4) Show that the set of all irrational numbers is uncountable. (5) Let C = {0, 1, 2, . . . , 9}. Show that the set C ×C × · · · is...
1) Write a function equation(A, B) that returns a value C where A + B =...
1) Write a function equation(A, B) that returns a value C where A + B = C a. Write the equation function: b: Write the output of your function for a few values: 2) Write a non-recursive function called fact(n) that computes n! Try computing fact(n) for large values. Can you find the maximum value for n your program will compute correctly? a) Write your fact function: b) Write the output of your function for a few values: 3) Write...
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.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT