-3

2 Loops - just a simple string reversal.

However, I have an issue when I use two different end states. I just wanna' know why it's not printing anything.

for (unsigned int i = a.length() - 1; i != -1; i--)
    {
        b += a[i];
    }
    cout << a << "\n" << b << endl;

vs

for (unsigned int i = a.length() - 1; i > -1; i--)
    {
        b += a[i];
    }
    cout << a << "\n" << b << endl;

The second will not print anything - looking for an explanation why.

Sorry for the noob question, but certainly curious. Thank you!

Travis
  • 55
  • 8
  • 3
    I'm pretty sure the first one will also not print anything. Think about what "unsigned" means. – Rakete1111 Jun 10 '17 at 19:19
  • Well, this first loop actually does print something. Just not sure why the second one isn't... For example: output for : "schamma doo" schammma doo ood ammmahcs Press any key to continue . . Oh! I simply changed it to a normally declared int and it worked. Thank you! – Travis Jun 10 '17 at 19:22
  • The first loop tests i for unequality with a very big number. The second tests, if i is greater than the biggest possible number. –  Jun 10 '17 at 19:25
  • 1
    Possible duplicate of [Unsigned and signed comparison](https://stackoverflow.com/questions/3384911/unsigned-and-signed-comparison) – Weak to Enuma Elish Jun 10 '17 at 19:25

1 Answers1

0

With an unsigned integral value unsigned int i, a condition like i > -1 can never become true, whereas a condition i != -1 can become true. The reason is that integral literal -1, when promoted to an unsigned value, is actually the same as MAX_UNSIGNED. And it is then easy to see that a value of type unsigned int can never be greater than MAX_UNSIGNED, whereas it can happen that it is != MAX_UNSIGNED.

Try the following code:

unsigned int ui = -1;
printf("ui:%u ; MAX_UNSINGED: %u\n", ui, UINT_MAX);

bool onegreaterMinusOne = (1 > ui);
printf("1 > -1 in unsigned realm? %s\n", (onegreaterMinusOne ? "True" : "False"));

unsigned int anotherUI = 0;
printf("0 != -1 in unsigned realm? %s\n", (anotherUI != ui) ? "True" : "False");

anotherUI--;
printf("--anotherUI != -1 in unsigned realm? %s\n", (anotherUI != ui) ? "True" : "False");

Output:

ui:4294967295 ; MAX_UNSINGED: 4294967295
1 > -1 in unsigned realm? False
0 != -1 in unsigned realm? True
--anotherUI != -1 in unsigned realm? False

A little bit subtle is the last thing, where unsigned int anotherUI=0; anotherUI--; is evaluated (this corresponds to the last step in your first loop), because one might ask if "an unsigned underflow is defined behaviour". The answer is yes, as taken from the C++-standard (and copied from this SO answer ):

[...] A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. (ISO/IEC 9899:1999 (E) §6.2.5/9)

So (unsigned)0 - (unsigned)1 equals -1 modulo UINT_MAX+1, or in other words, UINT_MAX.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58