Advertisements

Extension 1: Integer Operations


Extensions provide a more in-depth overview of various topics. Here, I present a more in-depth discussion behind primitive data types.

Type int

As mentioned before, integers consist of individual digits. You can prefix it with a minus sign (a hyphen) to make it negative. You cannot put spaces, commas, or periods inside ints.

When dividing an integer by another, integer division occurs. In this case, the remainder of the division is dropped. Therefore, the result of 8/5 is 1, not 1.6. A practical use of this is as follows:

int number = 1234;
int thousands = number / 1000;
NSLog ("@The thousands place of %d is %d.", number, thousands);

The output of the code above is

The thousands place of 1234 is 1.

In contrast, the modulus operator (%) is used to derive the exact remainder of integer division. Therefore, 8 % 5 = 3.

Extending the above example,

int number = 1234;
int thousands = number / 1000;
NSLog ("@The thousands place of %d is %d.", number, thousands);
int hundreds = (number % 1000) / 100;
NSLog ("@The hundreds place of %d is %d.", number, hundreds);
int tens = ((number % 1000) % 100) / 10;
NSLog (@"The tens place of %d is %d.", number, tens);
int ones = (((number % 1000) % 100) % 10);
NSLog (@"The ones place of %d is %d.", number, ones);

The output of the code is

The thousands place of 1234 is 1.
The hundreds place of 1234 is 2.
The tens place of 1234 is 3.
The ones place of 1234 is 4.

The code above is rather involved, but try to figure it out. Remember, the modulus operator extracts the remainder of the division that could have been placed in its place.

Advertisements
Leave a comment

5 Comments

  1. Ethan

     /  October 17, 2010

    Good tutorial so far. I have never really understood a practical use of the modulus operator. So simple!!!! Thanks! Keep going!
    -Ethan

    Reply
  1. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements
  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 222 other followers

  • Back to the Past

    September 2010
    S M T W T F S
    « Aug   Oct »
     1234
    567891011
    12131415161718
    19202122232425
    2627282930  
  • Time Machine

  • You count!

    • 621,851 views
  • Worldwide Stats

    free counters
  • Advertisements
%d bloggers like this: