Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

here is the code that I started. I am having a hard time trying to write the three codes in the program C: -For the

here is the code that I started. I am having a hard time trying to write the three codes in the program C:

-For the Is Negative function, it will accept an integer as an argument, and it should return a bool (boolean). You are only allowed to use bitwise operations and your knowledge of how Twos Complement Binary works. For the function, you do not necessarily need to return a bool. You can instead just return an int. If that int is 0, the returned value will be false, otherwise, it will be true. This fact makes it easier to implement such a function. You should not assume that an integer is 32-bits, and should instead use some of the values computed at the top of the file (INT_MIN, INT_MAX, etc.).

-For the Twos Complement function, it will take an integer as an argument, and it must return an integer whose binary value is the Twos Complement of the integer passed in. You may do this however you can think of doing it, however you should not assume that an int is 32-bits (though you may assume that it is a Twos Complement binary number). Hint: Maybe the CPU can do this for you

-For the last function, it should accept an integer, and return 10 times that integer. The catch: you cant use * (multiplication), and you cant do x+x+x+... or use any for loops. You may only use addition and shifting, no loops or if statements. In particular, consider what shifting a binary number left does: 0011 << 1 = 0110. Consider what happens when you shift a decimal number to the left (say 23 << 1 = 230). Why does it multiply the number by 10? Does something similar happen in binary? Hint: it is possible to implement this using 2 shift operations, and 1 addition.

#include

const unsigned UNS_MAX = (unsigned)(-1); // 1111... const unsigned UNS_MIN = 0; // 0000... const int INT_MAX = UNS_MAX >> 1; // 0111... const int INT_MIN = ~INT_MAX; // 1000...

bool is_negative(int x)

{ }

int twos_complement(int x)

{ }

int times_ten(int x)

{ return (x<<1)+(x<<3); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions