Question: For this task, you will need to write three functions. One which returns whether an arbitrary int is negative or not, one which applies Twos
For this task, you will need to write three functions. One which returns whether an arbitrary int is negative or not, one which applies Twos Complement to an arbitrary int (which could be positive or negative), and another which multiplies an arbitrary int by 5.
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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
