Using bitwise operators to determine if one number is greater than another

This is a discussion on Using bitwise operators to determine if one number is greater than another within the C Programming forums, part of the General Programming Boards category; All, First post. Hope to stick around. I got an assignment yesterday dealing with bits and bytes and C. There's ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Using bitwise operators to determine if one number is greater than another

    All,

    First post. Hope to stick around.

    I got an assignment yesterday dealing with bits and bytes and C. There's a variety of functions we have to write that replicate operators using bitwise operations. The one I'm stuck on (this time, anyway...) is one that accepts to integers and then returns 1 if the first is greater than the second.

    We're only allowed to use bitwise operators along with + and !, and the code has to be straight line. No functions, loops, etc. No unsigned ints permitted.

    Here's what I came up with so far:

    Code:
    int greaterThan(int first, int second){
         // first > second means second - first is less than 0
        // shift the sign bit and then compare it to 1
        return (second + (~first +1)) >> 31 & 1;
    }
    This works fine in a lot of cases, but I think when I get to values like the negative max, the output is incorrect. I'm assuming it has to do with some kind of overflow on the addition, but I'm unsure of how to actually fix it without the use of conditionals.

    Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It will also fail for any variable that is not 32 bits in size.
    Last edited by CommonTater; 10-21-2010 at 11:02 PM.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,897
    If (first + second) is less than either operand then you know the comparison will overflow. That's the trouble with the math for this. You could return an overflow code in that case. The real operation never fails because it boils down to a processor instruction. Understanding how that works involves understanding ALUs. So there you go.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,072
    No unsinged ints?! Wow, that's harsh.

    By the way, don't just assume it will or wont work, write a bunch of unit tests like this:
    Code:
    assert(greaterThan(0x80000000, 0x7FFFFFFF) == 1);
    assert(greaterThan(0x7FFFFFFF, 0x80000000) == 0);
    Make sure you also have cases that test with both inputs being equal.

    When they all pass, you're either done or you're tests are inadequate.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Quote Originally Posted by CommonTater View Post
    It will also fail for any variable that is not 32 bits in size.
    Yes, I know. It's an assumption we're allowed to make.
    Quote Originally Posted by whiteflags View Post
    If (first + second) is less than either operand then you know the comparison will overflow. That's the trouble with the math for this. You could return an overflow code in that case. The real operation never fails because it boils down to a processor instruction. Understanding how that works involves understanding ALUs. So there you go.
    What do you mean "less than either operand"? Also, I understand that the math
    doesn't literally stop working. But I still have to spit out the right answer, and if I remember correctly, we can't throw an overflow code.


    Quote Originally Posted by iMalc View Post
    No unsinged ints?! Wow, that's harsh.

    By the way, don't just assume it will or wont work, write a bunch of unit tests like this:
    Code:
    assert(greaterThan(0x80000000, 0x7FFFFFFF) == 1);
    assert(greaterThan(0x7FFFFFFF, 0x80000000) == 0);
    Make sure you also have cases that test with both inputs being equal.

    When they all pass, you're either done or you're tests are inadequate.
    If it wasn't harsh, I wouldn't be posting here

    And yes, I know that we should test with a variety of cases. I also know that it fails when using +/- max/mins of int. I know THAT it fails, I'm just unsure of how to make it...stop failing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  2. need help using bitwise operators
    By alexwink in forum C Programming
    Replies: 12
    Last Post: 11-08-2006, 02:36 PM
  3. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  4. bitwise operators
    By HelpPlease in forum C Programming
    Replies: 4
    Last Post: 10-05-2001, 07:40 PM

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