title: Equality operand behavior for unsigned and signed types
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5416414/signed-unsigned-comparisons">Signed/unsigned comparisons</a>  </p>
  </blockquote>
  
  
  
  <p>So I have the below code, which sets an unsigned int to a negative number, and then does a comparison of the unsigned int with the same negative number. The output I get is "Not Equal".  I understand that when setting the unsigned int to -1, the value of the unsigned int is set to 255 in this case. </p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  
  int main(int argc, char **argv)
  {
    uint8_t test = 0;
    int8_t set = -1;
  
    test = set;
  
    if (test == set) {
      std::cout &lt;&lt; "Equal";
    } else {
      std::cout &lt;&lt; "Not Equal";
    }
  }
  </code></pre>
  
  <p>However, what causes the equality operand to return false?  This seems to be in direct contrast with the answer to the similar question <a href="https://stackoverflow.com/questions/5416414/signed-unsigned-comparisons">Signed/unsigned comparisons</a> which states</p>
  
  <blockquote>
    <p>Many binary operators that expect operands of arithmetic or
    enumeration type cause conversions and yield result types in a similar
    way. The purpose is to yield a common type, which is also the type of
    the result. This pattern is called the usual arithmetic conversions,
    which are defined as follows:</p>
    
    <p>If either operand is of type long double, the other shall be converted
    to long double.</p>
    
    <p>Otherwise, if either operand is double, the other shall be converted
    to double.</p>
    
    <p>Otherwise, if either operand is float, the other shall be converted to
    float.</p>
    
    <p>Otherwise, the integral promotions (4.5) shall be performed on both
    operands.54)</p>
    
    <p>Then, if either operand is unsigned long the other shall be converted
    to unsigned long.</p>
    
    <p>Otherwise, if one operand is a long int and the other unsigned int,
    then if a long int can represent all the values of an unsigned int,
    the unsigned int shall be converted to a long int; otherwise both
    operands shall be converted to unsigned long int.</p>
    
    <p>Otherwise, if either operand is long, the other shall be converted to
    long.</p>
    
    <p>Otherwise, if either operand is unsigned, the other shall be converted
    to unsigned.</p>
  </blockquote>
  
  <p>Thus according to this answer,  since both operand is unsigned, the other shall be converted to unsigned, and the equality should pass. But it clearly is not passing, and as answered already in this question, they are both being promoted as signed ints. </p>