Write code to implement the following function:
/*
* Generate mask indicating leftmost 1 in x. Assume w=32.
* For example, 0xFF00 -> 0x8000, and 0x6600 — > 0x4000.
* If x = 0, then return 0.
*/
int leftmost_one(unsigned x);
Your function should follow the bit-level integer coding rules (page 128), except that you may assume that data type int has w = 32 bits.
Your code should contain a total of at most 15 arithmetic, bitwise, and logical operations.
Hint: First transform x into a bit
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: Program... -Access
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out with Python (4th Edition)
Concepts of Programming Languages (11th Edition)
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
- Write a C-function with two arguments (n and r) that has prototype: char clearbit(char k, char bits) The function clears (sets to 0) the bit number k (in the range of 0 to 7) in bits and returns the resulting value. For example, if k is 0x02 and bits is 0x07, the function would return bits with its k’th bit cleared, resulting in 0x03. It must not change other bits in bits. Hint: You may use any number of C-statements, but this task can be accomplished in as few as one!arrow_forwardWrite a function int bitset(int x, int n) to set nth bit of x to 1, if its (n-1)th bit is 1. if n <1 or n >15, the function will return -1 to thecalling functionarrow_forwardImplement the quadratic_formula() function. The function takes 3 arguments, a, b, and c, and computes the two results of the quadratic formula: x1=−b+b2−4ac2a x2=−b−b2−4ac2a The quadratic_formula() function returns the tuple (x1, x2). Ex: When a = 1, b = -5, and c = 6, quadratic_formula() returns (3, 2). Code provided in main.py reads a single input line containing values for a, b, and c, separated by spaces. Each input is converted to a float and passed to the quadratic_formula() function. Ex: If the input is: 2 -3 -77 the output is: Solutions to 2x^2 + -3x + -77 = 0 x1 = 7 x2 = -5.50 code: # TODO: Import math module def quadratic_formula(a, b, c):# TODO: Compute the quadratic formula results in variables x1 and x2return (x1, x2) def print_number(number, prefix_str):if float(int(number)) == number:print("{}{:.0f}".format(prefix_str, number))else:print("{}{:.2f}".format(prefix_str, number)) if __name__ == "__main__":input_line = input()split_line = input_line.split(" ")a =…arrow_forward
- Write a program that swaps 5th~11th bits in data_a with 25th~31th bits in data_bYour program must work for any data given, not just the example belowIn this question, we assume that the positions of bits count from right to left.That is, the first bit is the least significant bit.data_a DCD 0x77FFD1D1data_b DCD 0x12345678arrow_forwardSuppose we number the bytes in a w-bit word from 0 (least significant)to w/8 −1 (most significant).Write code for the following C function, which will return an unsignedvalue in which byte i of argument x has been replaced by byte b:unsigned replace byte (unsigned x, int i, unsigned char b);Examples:replace byte(0x12345678, 2, 0xAB) → 0x12AB5678replace byte(0x12345678, 0, 0xAB) → 0x123456ABarrow_forwardIn c++ Now write a program for a double floating pointtype.•What are the number of significant bits that the double’s mantissa can hold, excluding the sign bit.•What is the largest number that a double’s mantissa can hold without roundoff error?•Repeat the program shown on the previous page but set to show where the double’s mantissa starts to exhibit roundoff errors.arrow_forward
- Build a _lengOfSt function in MIPS, that takes an argument in $a1 which is the address of a null-terminated string, returning the length of the given string (number of characters excluding the null-character) in $v0.arrow_forwardCompute the weakest precondition for each of the following assignmentstatements and postconditions:1. a = 2 * (b - 1) - 1 {a > 0}2. b = (c + 10) / 3 {b > 6}3. a = a + 2 * b - 1 {a > 1}4. x = 2 * y + x - 1 {x > 11}arrow_forwardWrite a MIPS procedure that takes as its three parameters the address of a zero-terminated string, a character c, and an integer n, and returns the number of distinct runs of consecutive instances of c in the string that are of length at least n. Also, write a simple main program to test your procedure. Your main program should input a string from the user (you may assume that the string has at most 20 characters), input the character and the integer, invoke your procedure, output the return value, and then terminate. For example, given the inputs “bbabbbbagbgbb”, “b”, and 2, the output should be 3. Your code must use the “standard” conventions covered in class for passing parameters and returning results. code in mipsarrow_forward
- Implement the following error detection and error correction algorithms using C programming for Hamming code. For Hamming codes, flip a bit in the data and implement the algorithm to correct the same on the receiver side.arrow_forwardImplement the modular exponentiation (a.k.a. fast exponentiation) function mod_exp (b, n, m) to compute bn (mod m) more efficiently. (Hint: to read n bit-by-bit, use / and % operations repeatedly) a) Test your function for b = 3, n = 231 – 2, m = 231 – 1. b) Report the result and the time (in seconds) it takes to find the result. Q3. [30 pts] Modify your is_prime function to use the mod_exp (b, n, m) instead of the standard power operation (b**n % m). Rename it as is_prime2. Modify the mersenne (p) function to use is_prime2, and call it mersenne2. a) Use the modified function mersenne2 to print all the Mersenne primes Mp for p between 2 and 31 if possible, (with k = 3 in the is_prime function). Compare the results with the ones found in Q1. b) Gradually increase the range of p to find more Mersenne primes (say up to p = 101 if possible). What is the largest Mersenne prime you can achieve here? c) Extend the work in part (b) and find the maximum Mersenne prime you can get from this…arrow_forwardWrite a program that swaps 5th~11th bits in data_a with 25th~31th bits in data_bYour program must work for any data given, not just the example belowIn this question, we assmue that the positions of bits count from right to left.That is, the first bit is the least significant bit.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning