Thread: Using bitwise operator to see if x>y

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Using bitwise operator to see if x>y

    If x > y, then this function will return 1, other wise return 0.
    so far i have
    int isitGreater(int x, int y) {

    return (((y+((~x)+1)) >> 31) & 1);
    but it's not working.
    Im sure i have the logic right, if X - Y and I get a negative number, that means y > x , so therefore the 32nd bit is a 1, so i shift that bit to the right 31 times and then "and" it with "1".
    why isnt it working?

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    It's working fine

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    Quote Originally Posted by sana.iitkgp View Post
    It's working fine

    not if X is negative and causes a overflow

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Obviously it'll fail if x is INT_MIN, but otherwise it seems okay.
    Give some specific values for which it fails.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    if X is -4 and y is 5

    this will cause an overflow in my code but i dont know how to handle this overflow error without using if/while statements

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    or: error i have is: (-2147483648[0x80000000],2147483647[0x7fffffff]) fail

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What exactly is the purpose and the constraints of this exercise?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    To only use : Legal ops: ! ~ & ^ | + << >>

    and no conditional statments, working with bitwise operators

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    For x=-4 and y=5 it is working fine for me.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Lina_inverse View Post
    if X is -4 and y is 5

    this will cause an overflow in my code but i dont know how to handle this overflow error without using if/while statements
    Take x is -4, y is 5
    Where's the overflow?
    It seems to give the correct answer 0.

    Your second example (-2147483648) is INT_MIN, which I pointed out won't work since you can't take the 2's complement of it (i.e., there's no corresponding positive number).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    how should I fix my second error? and what do you mean by INT_min?

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    INT_MIN is the minimum integer that can be represented by the int data type.
    It depends on the number of bits for int and whether it is signed/unsigned.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by sana.iitkgp
    It depends on the number of bits for int and whether it is signed/unsigned.
    int is always signed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> how should I fix my second error?
    Use a larger integer type to find the result.

    >> and what do you mean by INT_min?
    The type int is the default integer type, usually but not necessarily the same size as a WORD on the machine architecture. INT_MIN has to stand for the smallest value that can be represented by this type, and must be at least (-2)^16 IIRC.

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Hey,

    I have to admit that I have a soft-spot for bitwise operations.

    I have a few suggestions for you
    Code:
    ((~x)+1))
    This is the 2's complement that is a lot easier to write like this:
    Code:
    (-x)
    And instead of saying "31", you can include <limits.h> and use this instead
    Code:
    sizeof(int) * CHAR_BIT - 1
    This leaves your subroutine looking like this:
    Code:
    int isitGreater(int x, int y)
    {
        return (((y-x) >> (sizeof(int) * CHAR_BIT - 1)) & 1);
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operator question
    By f1gh in forum C Programming
    Replies: 1
    Last Post: 12-25-2010, 12:31 PM
  2. bitwise operator
    By donkey_C in forum C Programming
    Replies: 7
    Last Post: 09-26-2010, 02:47 PM
  3. Bitwise Operator Help
    By ggraz in forum C Programming
    Replies: 2
    Last Post: 09-28-2008, 09:20 AM
  4. what does this mean, bitwise operator
    By InvariantLoop in forum C++ Programming
    Replies: 11
    Last Post: 03-09-2006, 07:43 PM
  5. bitwise operator
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 09-30-2005, 01:17 AM

Tags for this Thread