title: Make a function to return 1 if x <= y, using only bitwise operators
body: <p>this is homework and I'm struggling with it along with at least the half of my class that I can see is here. Anyways. I need to make a function that <code>returns 1 if x &lt;= y else return 0</code>. </p>
  
  <p>We can only use: <code>! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</code> and the max ops count is: <code>24</code>
  All integers must be <code>signed</code> and there cannot be any calls made to any other functions that I've created or that C offers. We are allowed to assume all integers are <code>32 bits</code> and they must be signed.</p>
  
  <p>I'm unsure of where to begin with this function. so far I have the following:</p>
  
  <pre><code>int isLessOrEqual(int x, int y) {
      int diff = (y+~x+1); //The same as y-x
      int diffSign = (diff&gt;&gt;31) &amp; 1; //if negative, this will be 1. Else it'll be 0.
      return !diffSign;
  }
  </code></pre>
  
  <p>This seems like it would work. But It doesn't work for certain inputs (according to the program that does the grading).</p>
  
  <p>Here is a function that does work that I found online but I do not want to copy it. I would like to understand it and see why it works and mine does not. It seems that my code does not work when it overflows, and that needs to be handled.</p>
  
  <pre><code>int isLessOrEqual(int x, int y) {
    int sign, isLess, dif, equal, isLessorEqual;
  
    sign = x ^ y;
    isLess = ~sign;
    dif = y + (~x + 1);
  
  
    equal = !dif;
  
    sign = sign &amp; x;
  
    dif = ~dif;
  
     isLess = isLess &amp; dif;
     isLess = isLess | sign;
     isLess = isLess &amp; (1 &lt;&lt; 31);
  
     isLessorEqual = (!!isLess) | (equal);
  
     return isLessorEqual;  
  }
  </code></pre>
  
  <p>If someone could help me <strong>understand</strong> the differences or how these functions work and handle overflow, that would be greatly appreciated and is what I am asking for. I'm close, just can't figure out this overflow thing</p>