Like Tree1Likes

Using bitwise operator to see if x>y

This is a discussion on Using bitwise operator to see if x>y within the C Programming forums, part of the General Programming Boards category; If x > y, then this function will return 1, other wise return 0. so far i have int isitGreater(int ...

  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
    21,582
    What exactly is the purpose and the constraints of this exercise?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,582
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,655
    >> 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,343
    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

Page 1 of 3 123 LastLast
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, 11:31 AM
  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, 06: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21