Question: Bit manipulation For this task, copy this code into a new tab of repl.it: #include const unsigned UNS_MAX = (unsigned)(-1); // 1111... const unsigned UNS_MIN
Bit manipulation
For this task, copy this code into a new tab of repl.it:
#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) { return x; // remove this line } int twos_complement(int x) { return x; // remove this line } int times_ten(int x) { return x; // remove this line } -
Run the code. You should see that it continues to prompt for an int until you input 1234. Every time you input an int, it will print the binary representation of the number, as well as the expected output (along with the actual output) of the functions you implement. Note, you can also input negative numbers to see what happens.
-
Do NOT edit any of the driver code beyond the large comment
-
You may do so if you want to test something out, however make sure it is in its original state when you are being graded
-
-
Implement the functions as specified. It is possible to implement all of them with very few lines of code (dont overthink it, and use your knowledge of how Twos Complement binary numbers are structured).
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
