small medium large xlarge

Generic-user-small
17 Dec 2010, 17:40
Ric Levy (21 posts)

Hi Tim,

I’m doing the exercise where we learn about signed and unsigned numbers. Just out of curiosity, I tried using a negative number with “unsigned int” … and it worked perfectly!

Is this because they have made it foolproof in case people forget? But in that case, is there a real difference between “int” and “unsigned int”?

Thanks!

Generic-user-small
17 Dec 2010, 17:50
Ric Levy (21 posts)

Oops - I was rushing ahead there. It turns out I just hadn’t changed %i to %u.

Of course that brings up the question of why I need to change both for it to truly operate as unsigned (surely once an unsigned variable, always an unsigned variable?), but no doubt that will become obvious in due course!

Generic-user-small
17 Dec 2010, 18:23
Ric Levy (21 posts)

OK, I think I get it. The %u goes in the text where you want the variable to appear, and then at the end of the line you specify which exact variable you were talking about.

So this thread is basically an insight into my slow thought processes :)

Generic-user-small
17 Dec 2010, 18:29
Tim Isted (105 posts)

Hi Ric,

When you change the type of the variable, it alerts the compiler as to what type of value should be interpreted from the 1s and 0s in the relevant area in memory when you make use of the variable from standard code.

When you supply a value to NSLog() using its format string, the behavior is a little different due to various internal details. By using a format specifier of %i, you’re asking for the 1s and 0s in memory to be interpreted as a signed integer. Use a specifier of %u and you’re asking for the same 1s and 0s to be interpreted as an _un_signed integer. It doesn’t actually matter what type of variable you originally declared as NSLog doesn’t get to see that type, it only gets to see the format specifier token, and the contents in memory that you’ve asked to be shown.

So, if you declare an unsigned int, then assign it a negative number value, then view that memory as if it were a standard int using %i, you’ll see the negative number.

If you change the %i to %u for display of a negative number, you won’t get the value you expect, you’ll see the value that results from the combination of 1s and 0s you set for a signed number but interpreted as if they were for an unsigned number. For added amusement, try declaring a float variable (floating point i.e., number with a decimal like 3.14) and viewing it as if it were an integer with the %i token.

This would probably all be less confusing if the compiler warned you when you assign a negative number to an unsigned variable, but on standard settings, it doesn’t. You can enable a warning by setting a compiler flag of -Wconversion. This will trigger a warning of “Negative integer implicitly converted to unsigned type” for your example, but the flag will likely cause a whole bunch of other warnings to appear for code in the rest of the project (turning this on for the project I currently have open in Xcode generates 417 warnings…!).

Tim

Generic-user-small
17 Dec 2010, 19:39
Ric Levy (21 posts)

OK, so just to put that in my own words so I understand…

When I create the variable with the command “Int”, it is telling the compiler that it’s a signed variable. But NSString doesn’t know that - it’s just regurgitating whatever’s in memory, so I have to use %i so it interprets what it finds as an integer. Got it!

By the way, thanks for the detailed explanation, and congratulations on the book! I’ve tried out a couple of books and podcasts, and this is the first thing that genuinely assumes no knowledge, and explains everything in a way that makes sense. No mean feat! Just from what I’ve learned so far (up to Chapter 5) I reckon I could already write a useful utility to do a simple operation like convert Fahrenheit to Celsius, which gives me quite a sense of achievement!

Generic-user-small
17 Dec 2010, 20:17
Tim Isted (105 posts)

Yes—regurgitating memory is exactly the right way to think about this.

And, writing small utilities is the best possible thing to do while you’re reading the book. Conversion apps are really useful examples, too. Once you’ve got through a few more chapters, you should have picked up enough to pull values from text fields in a user interface, perform calculations like Celsius-Fahrenheit conversion, then display the result in another field in the user interface.

Once you’ve done that, try a utility that uses e.g., a provided amount in USD currency, and a user-specified exchange rate, perhaps to convert to UK GBPounds, or Euros.

Good luck!
Tim

You must be logged in to comment