C Homework Comparisons: Answers & Explanations

  • Thread starter fsbadr
  • Start date
In summary: The explanation is that on the RHS we subtract a negative number which results in a positive number, thus not equal to the LHS.
  • #1
fsbadr
18
0

Homework Statement



This is a homework assignment. I have attempted the questions with the answers. If a review could be done, and if there is a need for change, I would appreciate the guidance.

int n1 = -1;
int n2 = 2;
unsigned int u1 = (unsigned int) n1;
unsigned int u2 = (unsigned int) n2;


/*Question
Is result always 1? Explain.
int result = (n1 < n2) == (-n1 > -n2);
printf("(%d < %d) == (-%d > -%d) evaluates to %d\n",n1, n2, n1, n2, result);
*/

The result will always be 1. To illustrate, n1 = -42, n2= -36

Left Hand Side (LHS):
n1 < n2 = -42 < -36 (which evaluates to true).

Right Hand Side (RHS)
(-(-42) > -(-36)) = 42 > 36 (which evaluates to true).

Since both sides are true the answer is 1 (true).

A second case is n1 = 15672, n2 = 12

LHS:
n1 < n2 = 15672 < 12 = false

RHS:
-n1 > -n2 = -15672 > -12 = false

Since both sides are false then this equals 1 (True).

A fundamental reason for this is due to the fact that we treat the LHs as a positive with a < sign
and on the RHS we reverse the sign but at the same time we also change the sign to >, thus both sides are effectively the same.

********************************************************************
/*Question
Is result always 0? Explain.
result = ~n1 + ~n2 == ~(n1 + n2);
printf("(~%d + ~%d) == ~(%d + %d) evaluates to %d\n",n1, n2, n1, n2, result);
*/

It is very difficult to assume that it will always be true. Let's illustrate.
For this I have assumed n1 = 187, n2 = 45. In binary we say that 187 = 1011 1011 and 45 = 0010 1101.

The ~ is a unary operator and inverts the bits (one's complement).

LHS:
~n1 + ~n2 we will first invert both 187 and 45 bits.
~187 = 0100 0100
~45 = 1101 0010. Performing addition on them = 100010110 = 278

RHS:
~ (n1 + n2) = ~ (187 + 45) = 232 which in binary is 1110 1000, when we invert the bits we get 0001 0111 which in decimal is 23

Hence LHS <> RHS = false = 0

The reason they will be 0 is due to the treatment of both variables on their respecive sides. LHS deals with inversion of bits on both n1 and n2.
They will be inverted first and then will be added. On the LHS because of parantheses, they will be added first and the resultant sum's bits will be inverted.

******************************************************************** */Question
result = ((n1 >> 1) << 1) <= n1;
printf("((%d >> 1) << 1) <= %d evaluates to %d\n",n1, n1, result);
Is result always 1? Explain.

A right shift is indicative of division, hence the resulting will be smaller, while the left shift implies multiplication resulting in a larger number.
We can illustrate with the following example.

n1 = 187

LHS:
((n1 >> 1) << 1)= 187 in binary is 1011 1011, doing a right shift will then result in 0101 1101 which in decimal is 93 hence (n1 >> 1) = 93.
We then do a left shift on 93. 93 in binary is 1011101. A shift on the left will result in 186. Hence:
((n1 >> 1) << 1) <= n1 = 186 <= 187 = 1 (True).

n1 is never changed, when we right shift the resulting value is then left shifted and compared with n1
which will always result in a smaller number hence it will always be 1.
*/

********************************************************************

/* Question Is result always 1? Explain.
On left hand side, the expression (int) (u1 - u2) means that the subtraction of u2 from u1 is done and then this result is cast to an int, that is, a signed int.
result = ((int) (u1 - u2)) == -(n2 - n1);
printf("((int) (%u - %u)) == -(%d - %d) evaluates to %d\n",u1, u2, n2, n1, result);
*/


The result will always be 1. We can illustrate with the following example:

n1 = -1, n2 = 2, u1 = -1, u2 = 2

because unsigned integers are not used for negative integers. on LHS:
n1 which is -1, will result in 4294967295 (the limit of approx 4.3 billion - 1).
LHs would be (4294967295 - 2) = 4294967293. This is then cast to a signed int.
Since the max limit of the signed int is 2147483647, this is more than its limit. Hence it does a
double subtraction of 2147483647 which results in the value -1 (which it originally is), so the LHS looks like (-1 -2) = -3.

RHS = - (2 - (-1)) = - (2 + 1) = -3 = LHS = RHS = 1 = True.

We treat both sides the same EXCEPT on the RHS we change the sign of the resulting sum and on the right if there is a negative number the
unsigned integer converts it which, in turn, if is beyond the range of the int casting, is converted back to its original value.


********************************************************************
 
Physics news on Phys.org
  • #2
/*Question Is result always 1? Explain. On right hand side, the expression (unsigned int) (n2 - n1) means that the subtraction of n2 from n1 is done and then this result is cast to an unsigned int. result = ((unsigned int) (n2 - n1)) == u2 - u1; printf("((unsigned int) (%d - %d)) == %u - %u evaluates to %d\n",n2,n1,u2,u1,result); */ The result will always be 1. We can illustrate with the following example: n1 = -1, n2 = 2, u1 = -1, u2 = 2 LHS: (2 - (-1)) = 3 This is then cast to an unsigned integer which results in 3 (which it originally is). RHS: (2 - 4294967295) = -4294967293. Since it is a signed integer, the max limit is 2147483647 and if the value is more than the max limit the resulting number is double subtracted which results in the original number i.e. -1. Hence (3 == -1) = False = 0.
 
  • #3


it is important to always provide thorough explanations and reasoning for any conclusions or solutions. In this case, the individual has attempted to answer the questions and provided explanations for each result. However, it may be beneficial for the individual to also provide some background information on the topic, such as the purpose of the homework and the concepts being tested. Additionally, it would be helpful to provide more examples and potential edge cases to further support the explanations and ensure a complete understanding of the topic. Overall, the response provided is a good start, but could benefit from more context and elaboration.
 

What is the purpose of comparing C homework answers?

The purpose of comparing C homework answers is to check for accuracy and understanding of the concepts being taught. This allows students to identify any mistakes and learn from them, as well as to see how their solutions compare to others.

How can I improve my C homework comparison skills?

To improve your C homework comparison skills, it is important to have a strong understanding of the concepts being taught. Practicing regularly and seeking feedback from peers or instructors can also help improve your comparison skills.

What should I look for when comparing C homework answers?

When comparing C homework answers, you should look for correct syntax, logic, and efficiency in the code. It is also important to check for any errors or bugs, as well as to understand the reasoning behind different approaches to solving the problem.

Are there any resources available to help with C homework comparisons?

Yes, there are many online resources and forums where you can find C homework assignments and solutions to compare to. Your instructor or classmates can also be valuable resources for feedback and assistance with comparisons.

Why is it important to compare C homework answers with others?

Comparing C homework answers with others helps to identify any mistakes or misunderstandings in your own solutions. It also allows for exposure to different approaches and techniques, which can improve your understanding and problem-solving skills.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
19K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Introductory Physics Homework Help
Replies
2
Views
632
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
Back
Top