1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comparisons in C

  1. Jun 23, 2008 #1
    1. The problem statement, all variables and given/known data

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


    ********************************************************************
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?



Similar Discussions: Comparisons in C
Loading...