Question

PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...

PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP

For the following C functions:

int q3(int x) {

    return ((~x)>>31) & 0x1;

}

int q4(int x) {

    int mask = x>>31;

    int y= (x ^ mask);

    int z = (~mask + 1);

    return (y+z);

}

int q5(int x){

    int y = !x;

    int z = x >> 31;

    z = z | y;

    return !z;

}

int q6(int x) {

    int result = (x<<31);

    result = (result >> 31);

    return result;

}

---------

Part i) Explain what each function does without running the code. EXPLAIN YOUR ANSWER OR YOU WILL NOT RECEIVE CREDIT.

Part ii) For each of the above functions, write a simpler version of the function (if there is one)

Homework Answers

Answer #1

IMP NOTE: int in c is of 4 bytes, ie 32 bits. The most significat bit (left most) will be 1 for -ve numbers and 0 for positive numbers.

Function q3 will return 1 for positive numbers or 0 and 0 for negative numbers

~x will compliment the number and >>31 will bring the MSB to the LSB and anding with 1 will return 1 for positive numbers and 0 for -ve numbers

Here is the simpler version:

int newq3(int x)
{
    if (x < 0)
        return 0;
    return 1;
}

q4 will always return the absolute value of the given number

mask will store the x shifter to 31 times so 1 for -ve numbers and 0 for +ve

y will be 0 for even -ve numbers and 1 for odd -ve numbers, and always 0 for +ve numbers

Then we complement the mask and add 1 to get the original number by in +ve format

Here is simpler version

int newq4(int x)
{
    if (x < 0)
        return x * (-1);
    return x;
}

q5 will return 1 for +ve numbers and 0 for -ve numbers and 0

y becomes 1 for 0 and 0 for -ve and +ve numbers

z become the msb digits of the numbers

then z = z|b becomes 1 if z or y is 1 ( for 0 or -ve numbers) and 0 for +ve numbers

!z becomes reverse of z

Here is the simpler version

int newq5(int x)
{
    if (x <= 0)
        return 0;
    return 1;
}

q6 returns -1 for odd numbers and 0 for even numbers

For even numbers the result clearly becomes 0 after shifting so it will remain 0 after another shift too

For odd numbers the lsb is always 1, so after left shifting 31 bits result will have 1 at the MSB and after right shifting 31 times 1 will come at lsb and sign will be preserved resulting in -1

Here is the simpler code:

int newq6(int x)
{
    if (x < 0)
        x = x * (-1);
    if (x % 2 == 1)
        return -1;
    return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;     for (i = 0; i < 32; i+=2)        if ((x & (1<          return 0;            return 1; } Legal ops: ! ~ & ^ | + << >> Max ops: 12 I have a few questions similar to this one, but I'm running into much the same problem for all of them. The behavior of this code appears to be that...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25...
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25 points) // Note: You may notice some warnings for variables when you compile in GCC, that is okay. #define macro_1(x) ((x > 0) ? x : 0) #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10) int function_1(int a, int b) { return (3*a - 3*b*b + 4*a * b - a*b * 10); } // Part 1: //...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
The economy is described by the following functions: C = 100 + 0.8Y D T x...
The economy is described by the following functions: C = 100 + 0.8Y D T x = 10 T r = 40 I = 60 + 0.1Y G = 80 Nx = 20 Notice that in this example investment is pro-cyclical. Q1. Write down the definition of aggregate demand, and a condition that describes equilibrium in the Keynesian Cross diagram Q2. Substitute all the information that you were given and find equilibrium output. Illustrate on the Keynesian Cross diagram. Q3....
Consider a closed economy that is described by the following functions: C = 20 + 0.5Y...
Consider a closed economy that is described by the following functions: C = 20 + 0.5Y D T x = 10 T r = 40 I = 100 ? 10 · i G = 40 where i is the interest rate in the economy. Q1. Suppose originally i = 1. Find equilibrium output. Illustrate on the Keynesian cross diagram. Q2.Suppose now the interest rate increases to i = 2. Find new equilibrium output. Illustrate the change on the Keynesian cross...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...