?
Solved

Logic problem

Posted on 2003-12-07
2
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 500 total points
ID: 9892317
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
ID: 9892338
sLine.lenth() was unsigned, but iNewPlace was signed. Nevertheless you  helped me out.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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