Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this task, copy this code into a new tab of repl.it: #include // Though the integers in C are typically 32-bit, it is not

For this task, copy this code into a new tab of repl.it:

#include // Though the integers in C are typically 32-bit, it is not // safe to assume as such, so your code should not assume // that the integers are 32-bits. The driver code is written // as such. The 'int' data type is an N-bit Two's Complement // binary number, and the 'unsigned' data type is a regular // N-bit binary number (no negatives). Again, N is likely 32, // but you should not assume this in your code.
// The comments after each of these lines show what the // underlying binary representation of these numbers look // like. The first (leftmost) digits are given, and the dots // represent a continuation of the last digit listed. These // constants might be useful. 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...
// This function shoulud return a boolean value that is true // if x is negative. You may only use bitwise operations, // you cannot use any comparison operations. Remember, an int // is automatically casted to bool using the rule if it's 0, // it's false, otherwise it's true. bool is_negative(int x) { return x; // remove this line }
// This function should return a value that when printed out // in binary (which has been provided to you), is the 32-bit // two's complement of the input x. You may use any method // you can come up with to do this, though the code should // be concise. int twos_complement(int x) { return x; // remove this line }
// This function should return 10 times the input x. You must // use both shift operations and addition, and nothing else. // x+x+x+... will NOT be accepted. int times_ten(int x) { return x; // remove this line }
/************************************************* * * * DO NOT MODIFY THE CODE BELOW FOR SUBMISSION * * * *************************************************/ // You may read this code if you want, but it is not necessary // for the assignment #include 
// Print out the integer x in binary // Note, you will need to implement this function in a later // lab, however you may not use this implementation (as it // violates the limitations for how the function must be // written). void print_bin(int x) { unsigned divisor = INT_MIN; while (divisor > 0) { if ((x / divisor) > 0) printf("1"); else printf("0"); x -= (x / divisor) * divisor; divisor /= 2; } }
void print_bool(int x) { if (x == 0) printf("false"); else printf("true"); }
// This function is an implementation of the two's complement // function. It is deliberately hyper-convoluted, and it // is highly suggested that you ignore it to avoid unnecessary // confusion. You may not copy this implementation or call // this function in your solution. // // Note: it is much easier to actually implement the function // than what is programmed here. Again, this is just written // to check your solution without giving away the expected // solution. int a(int b) { int d,e,f,g; for (int c = 14411; (~c - c) != -1; c >>= 2) { d = (b ^ 0) ^ (~b | 0) ^ (b & (-1)) ; e = c | 1; f = 1; while ((1 << f) > 1) { e |= (e << f); f <<= 1; } e = ~e; g = -~-~(-~-2); while (d & g) { e |= g; g <<= (~-~-3); } e |= g; b = d ^ e; } return b; }
// This is the driver for the code int main(void) { int x; printf("Input a number (1234 to exit): "); scanf("%d", &x); while (x != 1234) { printf("x Binary: "); print_bin(x); printf(" "); printf(" "); if (is_negative(x) != (x < 0)) printf("FAILED"); else printf("PASSED"); printf(": IS NEGATIVE "); printf("Expected: "); print_bool((x<0)); printf(" "); printf(" Got: "); print_bool(is_negative(x)); printf(" "); printf(" "); if (twos_complement(x) != a(x)) printf("FAILED"); else printf("PASSED"); printf(": TWO'S COMPLEMENT "); printf("Expected: "); print_bin(a(x)); printf(" "); printf(" Got: "); print_bin(twos_complement(x)); printf(" "); printf(" "); if (times_ten((unsigned)x) != x*10) printf("FAILED"); else printf("PASSED"); printf(": TIMES TEN "); printf("Expected: "); print_bin(x*10); printf(" (%d) ", x*10); printf(" Got: "); print_bin(times_ten(x)); printf(" (%d) ", times_ten(x)); printf(" "); printf("Input a number (1234 to exit): "); scanf("%d", &x); } }
  1. Do NOT edit any of the driver code beyond the large comment

    1. 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

  2. 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),

- one 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

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

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

Students also viewed these Databases questions

Question

=+j on to staff their operations in the global marketplace.

Answered: 1 week ago