• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Logic problem

      int k;
      int m;
      TRACE("%d ",!(iNewPlace>=(sLine.length()-1)));      //outputs 1

      TRACE("%d ",iNewPlace);      //outputs 0
      TRACE("%d ",sLine.length()-1);      //outputs -1

      TRACE("%d ",((iNewPlace>=(sLine.length()-1))));      //outputs 0

      TRACE("%d ",((m=iNewPlace)>=(k=(sLine.length()-1))));      //outputs 1

      TRACE("%d ",0>=-1);      //outputs 1

      TRACE("%d ",k>=m);      //outputs 1

Why is TRACE("%d ",((iNewPlace>=(sLine.length()-1))));      outputting false?
1 Solution
iNewPlace and sLine.length() are unsigned. 0 - 1 is 0xffffffff which is -1 in two's compliment (signed int) but is a really big number as an unsigned int.

When the values are converted to integers (%d conversion or assignement to a signed int) you get signed comparison. Try comparing iNewPlace with m and I'll bet you get a signed/unsigned comparison warning from the compiler.

Hope this helps,
ParticlenessAuthor Commented:
sLine.lenth() was unsigned, but iNewPlace was signed. Nevertheless you  helped me out.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now