Solved

# Logic problem

Posted on 2003-12-07
206 Views
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
Question by:Particleness

LVL 11

Accepted Solution

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

ID: 9892338
sLine.lenth() was unsigned, but iNewPlace was signed. Nevertheless you  helped me out.
0

## Featured Post

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.