Objective c introduction (pt 2)


Published on

Covers Storage, binary notation, two's complement, data types and modifiers

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

  • Objective c introduction (pt 2)

    1. 1. Objective-C: An Introduction (pt 2) Tennessee Valley Apple Developers Saturday CodeJam August 28, 2010
    2. 2. Storing Information Computers store information in two ways: Persistent storage: hard drives, optical drives, flash drives, etc. RAM: random access memory – temporary use while the computer is running
    3. 3. RAM: Bits and Bytes RAM is effectively a collection of switches, called “bits”. Each bit can represent two values: “on” (1) or “off” (0) Groups of bits are used to represent all characters; the standard group contains 8 bits and is called a “byte”
    4. 4. Numeral Systems Our numeric system consists of ten numbers (0 thru 9) which is called the decimal notation, or base 10, system. Computers have only two numbers to work with (0 and 1). This is called the binary notation, or base 2, system.
    5. 5. Storage Capability of a Byte A byte is capable of storing 256 different characters (28) Each character is represented numerically by a value ranging from 0 to 255 (28 - 1)
    6. 6. Storing Numbers in Memory The storage of numeric values depends on the system architecture and the operating system Today’s Mac systems are typically a minimum of 32-bit architecture; the newer Macs are 64-bit The operating system, however, may be running in 32-bit mode, even on hardware capable of supporting 64-bits.
    7. 7. What does this mean? 32-bit systems can store 4,294,967,296 different values (232) These values can range from 0 to 4,294,967,295 (232 - 1), or if working with signed numbers, -2,147,483,648 (231 x -1) to 2,147,483,647 (231 - 1). 64-bit systems can store 18,446,744,073,709,600,000 different values (264) These values can range from 0 to 18,446,744,073,709,599,999 (264 - 1), or if working with signed numbers, -9,223,372,036,854,780,000 (263 x -1) to 9,223,372,036,854,779,999 (263 - 1).
    8. 8. Why is this important? Because different scalar variable types will be allocated a set number of bits to store information, and the number of bits that get allocated and the way the numbers are stored will vary depending on the variable’s type. If you declare a variable of one type, and you store a different type of data in that variable, your results will at a minimum be unreliable, and more than likely your program will behave erratically and crash.
    9. 9. How are signed numbers stored? Signed numbers are stored in what is called the Two’s Complement System The first bit (most significant bit) is used to store the sign of the number – positive is 0, negative is 1 If the number is negative, it is the complement of the binary positive, minus 1
    10. 10. Examples: Positive & Negative Integers Positive Negative 1 0 0 0 0 0 0 0 1 -1 1 1 1 1 1 1 1 1 12 0 0 0 0 1 1 0 0 -12 1 1 1 1 0 1 0 0 21 0 0 0 1 0 1 0 1 -21 1 1 1 0 1 0 1 1 26 0 0 0 1 1 0 1 0 -26 1 1 1 0 0 1 1 0 32 0 0 1 0 0 0 0 0 -32 1 1 1 0 0 0 0 0 40 0 0 1 0 1 0 0 0 -40 1 1 0 1 1 0 0 0
    11. 11. Why use this? The computer’s circuitry does not have to examine the “sign” of the number for addition and subtraction, making the calculations much faster and more accurate while using less CPU cycles for the calculation “Faster Math with less effort”
    12. 12. In Addition Binary numbers are simply added together, with any carryover remainder getting dropped at the significant bit carryover 0 1 1 1 0 0 12 0 0 0 0 1 1 0 0 + 21 0 0 0 1 0 1 0 1 = 33 0 0 1 0 0 0 0 1
    13. 13. In Subtraction The number being subtracted is converted to the “two’s complement” and addition is performed using a “negative” number carryover X 1 1 0 0 0 1 1 1 33 0 0 1 0 0 0 0 1 -21 1 1 1 0 1 0 1 1 = 12 0 0 0 0 1 1 0 0
    14. 14. What is a “variable”? A variable is a location in RAM reserved for storing data used by an application. Each variable is given a name, assigned a type, and assigned a value. The name can be used by the source code to access the value assigned to the variable, either to read it or change it.
    15. 15. What is a “variable”? In order to use a variable, it must first be declared. When you declare a variable, you give it a name and define the type of data that will be stored in the variable. Once declared, you cannot subsequently change the data type (although you can change its behavior through “type casting”) When you assign data to a variable, it’s called initializing the variable.
    16. 16. What is a “variable”? A variable is a symbolic name given to a block of memory that holds information in memory that can be changed Variables are created by “declaring” them In Objective-C, variables are typically declared at the class level (available to all methods in the class), or at the method level (available only inside the method where they are declared). The availability of a variable is called its “scope”.
    17. 17. What does “scope” mean? The scope of a variable is effectively how long the variable will be available for your code to use General Rule: a variable is available only within the code block where it is declared Variables declared in the attribute section of a class definition (header file) are available to all methods within that class Variables declared inside a method are available only to that method
    18. 18. Objective-C Data Types Objective-C uses the following variable data types: int double char id float bool Objective-C also has qualifiers for data types that change them: long short long long signed/unsigned
    19. 19. Data Type: int The int data type (integer) can store positive or negative whole numbers (no decimals) The size of the number that can be stored is machine (CPU) and application dependent: Today’s Intel-based Macs can store at least 32-bit int values (-2,147,483,648 to +2,147,483,647; or 0 to 4,294,967,295 unsigned)
    20. 20. Data type: int By default, integers are stored in base 10, or decimal Integers can also be stored in octal (base 8) or hexadecimal (base 16) format Octal numbers are preceded by a leading zero (0) Hexadecimal numbers are preceded by zero-x(0x)
    21. 21. Data type: char The char data type is used to store a single character, such as a letter, numerical digit, punctuation or a space. char is also used to store a number of “special characters” that perform certain actions, such as tabbing, carriage returns, or beeps. Special characters are also called “escape sequences”.
    22. 22. Data type: char Some of the commonly used special characters are: a - beep t - horizontal tab b - backspace v - vertical tab f - form feed - backslash n - new line ” - double quotes r - carriage return ’ - single quote Using the backslash prefix is called “escaping the character”
    23. 23. Data type: float The float data type is one of the data types used to store floating point values (decimals). Float is typically referred to as “single precision”, and takes less space to store
    24. 24. Data type: double The double data type is also used to store floating point values, but takes twice the storage space as float Double is referred to as “double precision” By default, Objective-C defaults to the double data type when you declare “float” unless you specifically set the data store to the single- precision float type, by following the number stored in the variable with the letter “f”
    25. 25. Data type: BOOL The BOOL data type (or boolean) is used to store true / false conditions You can declare a variable’s data type as boolean by using either of the following keywords: BOOL _Bool
    26. 26. Data Type Qualifiers: long & long long The long qualifier is used to extend the value range of a data type For the int data type on today’s systems, int and long int have the same range; however you can extend the value range of int with long long long and long long can also be applied to the double data type
    27. 27. Data Type Qualifier: short The short qualifier can be used to reduce the storage space and range of the int data type, typically from 32-bits to 16-bits in width This will reduce the range of the signed integer values to -32,768 to +32,767
    28. 28. Data Type Qualifiers: signed / unsigned By default, all integer data types are assumed to be signed (used to store a positive or negative integer) If we know we only need a variable to store positive numbers, we can declare it as unsigned, which will effectively double its positive range
    29. 29. Data Type Qualifiers Unsigned can also be used with short integers When using unsigned, signed, short and long for integer variables, the int keyword is optional
    30. 30. What is a “constant”? A constant is similar to a variable in that it provides a named location in memory to store a data variable. The key difference is that, once a value has been assigned to a constant, it cannot be changed. Constants are useful for storing values that are used repeatedly throughout an application
    31. 31. Data type: id The id data type is a general purpose data type used to store a reference to an object, even if you don’t know what type of object you’re working with Each object that is created when the program is running will have its own unique id, which can be captured and stored in a variable of type id
    32. 32. What is a “pointer”? A pointer is a variable that contains the memory address of another variable. Pointers, like variables, are declared, and have a data type assigned at declaration Pointers, like variables, are also subject to the same scope rules Objective-C makes extensive use of pointers, especially for accessing objects Pointers are designated with the indirection operator (*) preceding the variable name in the declaration statement
    33. 33. To get the address of the pointer’s “object”... you can assign the address of the data object or scalar variable to an integer variable using the “&” symbol For Example: int anInt = 50; int *aPointertoAnInt = &anInt; If you display the value stored in aPointertoAnInt, you will see the hexadecimal address of the variable anInt.
    34. 34. What is “type casting”? Type casting is used to make a variable of one data type temporarily behave like a different data type The original value and data type do not change when a variable is type cast Type casting should be used with caution -- make sure you understand the impact on your resulting values when you cast a variable as a different data type