Solved

Logic problem

Posted on 2003-12-07
2
201 Views
Last Modified: 2012-05-04
      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

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

Why is TRACE("%d ",((iNewPlace>=(sLine.length()-1))));      outputting false?
0
Comment
Question by:Particleness
2 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 125 total points
Comment Utility
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,
-bcl
0
 

Author Comment

by:Particleness
Comment Utility
sLine.lenth() was unsigned, but iNewPlace was signed. Nevertheless you  helped me out.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now