The purpose of this assignment is to give you practice using C bitwise and shift
ID: 3834424 • Letter: T
Question
The purpose of this assignment is to give you practice using C bitwise and shift operators. Your assignment is to write the following functions. The body of each function should consist of only 1 C statement. The statement may not be an if statement, switch statement, or a loop. You may only use the following C operators: =, * (dereferencing, not multiplication), & (bitwise and, not the address operator), |, ~, ^, <<, >>, and +. You may not need to use every operator. You are also restricted in the constants that you may use, as specified below for each problem.
The functions that you must write are described below. For each, its prototype is given, as well as a description of its proper behavior, and any limitations on the constants that you may use to write the function. Whenever a description refers to the nth bit of an integer, it refers to the bit which represents 2 n ; thus bit 0 is the rightmost (least significant) bit. The sign bit is bit 31.
gcc –o hw4 hw4_test.c hw4.c ./hw4
My test code calls each of your functions on 100 test cases, and reports the number of incorrect answers produced by each function.
Each function is passed a parameter xptr, which is a pointer to an integer. There may be second parameter for some of the functions. For the sake of brevity in my descriptions below, we will assume that xptr points to an integer variable named x, and the parameter yptr (in the last problem) points to an integer variable named y.
void zero(int *xptr);
This function sets x to 0. You are not allowed to use any constants.
void maximum_int(int *xptr);
This function sets x to the largest integer (2147483647). You may only use positive constants less than 32.
Explanation / Answer
public class OddEvenSum { // Save as "OddEvenSum.java" public static void main(String[] args) { int lowerbound = 1, upperbound = 1000; int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int number = lowerbound; while (numberRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.