A.
Explanation of Solution
C expression for “
The C expression for “
Expression: -1 << k
For the above expression “-1 << k”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition for part A
int partA(int k)
{
//Returns the value of given expression
return -1 << k;
}
//Main function
int main(int argc, char* argv[])
B.
Explanation of Solution
C expression for “
The C expression for “
Expression: ~(-1 << k) << j
Program:
For the above expression “~(-1 << k) << j”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition for part A
int partA(int k)
{
//Returns the value of given expression
return -1 << k;
}
//Function definition for part B
int partB(int k, int j)
{
/* Returns the value of given expression by calling function "partA" */
return ~partA(k) << j;
}
//Main function
int main(int argc, char* argv[])
{
/* Call function "partB" with checking value using "assert" function */
assert(partB(24, 8) == 0xFFFFFF00);
return 0;
}
The given code is used to return the value of expression “~(-1 << k) << j”
Trending nowThis is a popular solution!
Chapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- (Numerical) Using the srand() and rand() C++ library functions, fill an array of 1000 floating-point numbers with random numbers that have been scaled to the range 1 to 100. Then determine and display the number of random numbers having values between 1 and 50 and the number having values greater than 50. What do you expect the output counts to be?arrow_forward(Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forwardGiven a double precision floating point number with a decimal point, write a function named prg_question 3 to return the whole number and decimal part of the number as a std::pair object of integers with first part being the whole number part and the second part being the fraction or decimal part. For example given the number 3.14, the "first" part of the pair object must be 3 and the "second" must be 14. Do the following: 1. Follow UMPIRE process to get the algorithm. Write only the algorithm as code comments 2. Implement your function 3. Test your function for numbers 3.14159 and 2.71828. Please follow the test criteria given in the code templatearrow_forward
- Code the following.arrow_forward*Code in Python A bit shift is a procedure whereby the bits in a bit string are moved to the left or to the right. For example, we can shift the bits in the string 1011 two places to the left to produce the string 1110. Note that the leftmost two bits are wrapped around to the right side of the string in this operation. Define two scripts, shiftLeft.py and shiftRight.py, that expect a bit string as an input. The script shiftLeft shifts the bits in its input one place to the left, wrapping the leftmost bit to the rightmost position. The script shiftRight performs the inverse operation. Each script prints the resulting string. An example of shiftLeft.py input and output is shown below: Enter a string of bits: Hello world! ello world!H An example of shiftRight.py input and output is shown below: Enter a string of bits: Hello world! !Hello worldarrow_forward: Write a program in C++ to input attendance as 0 (for absent) and 1 (for present) for 25 students in 32 lectures and store it in a 2-D array. After the input, write loops for calculating and displaying the index numbers of the students who has short attendance (i.e. less than 75%). Output should be like this:Please enter attendance of all 25 students for given lecture (total 32 lectures):Enter attendance of Lecture “1”Student 1: <user enters 0 or 1>Student 2: <user enters 0 or 1> and so on….arrow_forward
- 3. Write a PYTHON function called midpoint quad.py with signature def midpoint quad (func, a, b, N) #quad = midpoint quad ( func, a, b, N) # comments return quad where func indicates the name of a function, a and b are the lower and upper limits of integration, and N is the number of points, not the number of intervals. Test your midpointquad routine by computing f2rdx = 1. Even if you use only one interval (i.e. N = 2) you should get the exact answer because the midpoint rule integrates linear functions exactly. Use your midpoint routine to estimate the integral of the Runge function, f(x) = 1/(1+x2), over the interval [-5, 5]. The exact answer is 2 arctan (5). Fill in the following table, using scientific notation for the error values so you can see the pattern. Midpoint Result Error N h 11 1.0 101 0.1 1001 0.01 10001 0.001 Estimate the order of accuracy (an integer power of h) by examining the behaviour of the error when h is divided by 10. (In previous problems, we have estimated…arrow_forward4. Using bitwise operators In C code Write a function int negate(int n) that can negate an integer without using the - operator.arrow_forward6.use c code to Develop a code that gets two integers from the user and calculates and prints their least common multiplier or LCM. LCM is the smallest number that divides both given integers. Example. lcm (10, 15) = 30, lcm (5,7) = 35 and lcm (12, 24) = 24Hint: we know that lcm(x,y) = xy/gcd(x,y) where gcd is the greatest common divisor (discussed in class). Develop a gcd function in your code, get the two numbers from the user, call your function to calculate their gcd and then use the formula above to calculate and display their lcm GOOD LUCK! Reference This is a list of function prototypes of the C library functions presented in class. You may use any of these functions in your solutions (unless the requirements explicitly indicate otherwise). As you have been provided the function prototypes, you are expected to use the functions correctly in your solutions. double atof(char *string); int atoi(char *string); long atol(char *string); int fclose(FILE *filePointer); char *fgets(char…arrow_forward
- With C programming and without function and pointer Problem Statement You love binary numbers 0 and 1. Now,You are given a binary string S of size N. Now you need to tell total how many 01 and 10 pair exist in the given string. Note - A binary string is a string which contains only 0 and 1. And you have to consider 01 or 10 as consecutive. Input Format The first line will contain T, the number of test cases. The first line of each test case contains a positive integer N. The second line of each test case contains a string S, consisting only 0 and 1. Constraints 1 <= T <= 100 1 <= N <= 1000 Output Format Print the count of pairs. Sample Input 0 41210101111101061010113100210 Sample Output 0 7 4 1 1arrow_forwardPart 1: Write a function factorialFunc(n) that gets the positive integer n and calculates n! n! = n*(n-1)* (n-2)* (n-3) … * 3 * 2 * 1; Part 2: sin(x) can be approximately calculated using the following formula, where n! is factorial(n) – for example 3!=3*2*1 = 6 (the function in previous problem). The more terms we use in the series, the higher will be accuracy of the calculations. By using infinite terms in the series we will have the exact value. Write a program that gets x and calculates sin(x) using 5, 10, 20 terms, using C++arrow_forwardQuestion No 1. Develop a C program that will ask user to enter a string that includes numbers, alphabets and special characters and store this input into array of characters. Write a "STRING" named function to extract alphabets from the user-defined input string and print this string as a message. Program will also print the encoded message; the encoding criteria is to add 1 in every alphabet except 'z' or 'Z'. After printing the encoded message, program need to print this message in reverse order. Write a "DIGIT" named function to extract digits from the user-defined input string and print these digits. After printing the digits, program also need to print the sum of these digits as a single number. Output Sample: Enter the String: S$1$7h@e0h1r7#o2z7 Message: Shehroz Encoded Message: Tifispz Reverse Message: zorhehS Digits: 1701727 Sum: 25arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning