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
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think...
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think I understand the underlying concepts of each, but I want to be sure. I'm looking for a piece by piece break down of each function. Function 1 int logicalShift(int x, int n) { x >>= n; return x & ~(~(~0 << n) << (32 + ~n + 1)); Function 2 { int bang(int x) { int neg = ~x + 1; return ((~(x |...
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 =...
please explain how does the following C code work. a) double ldexp(double x, int exponent) is...
please explain how does the following C code work. a) double ldexp(double x, int exponent) is a C math function that returns x multiplied by 2 raised to the power of exponent. How many narrowing and how many promotions happen automatically in the following code line? Name them one by one and explain each one separately. float f = 2.2f * ldexp(24.4, 3.0 * 2) - 4.4; b) int function1(int num1, int num2) { int num = num1 ^ num2;...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere else. int f2(int x, int y) { int i, z = 0; for (i = x; i <= y; i++) z = z + f1(i, 5); return z; }
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...
BASIC c++ question Please do not try to fix this, I just have a question regarding...
BASIC c++ question Please do not try to fix this, I just have a question regarding the function of this code. I understand this is an infinite loop, however, why is it that when I enter '0', it continues to ask the user for an input, but when I enter a letter, it continues infinitiy. And when it does infinitely loop, why is it that it continues to add 1 to each number? Doesn't the loop go through each line...
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...
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...