An Introduction (pt 2)
Tennessee Valley Apple Developers
August 28, 2010
Computers store information in two
Persistent storage: hard drives,
optical drives, flash drives, etc.
RAM: random access memory –
temporary use while the computer is
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”
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.
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)
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.
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).
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
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
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
“Faster Math with less effort”
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
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
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
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.
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.
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”.
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
Objective-C Data Types
Objective-C uses the following variable
Objective-C also has qualifiers for data
types that change them:
long long signed/unsigned
Data Type: int
The int data type (integer) can store
positive or negative whole numbers (no
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
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
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”.
Data type: char
Some of the commonly used special
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”
Data type: float
The float data type is one of the data
types used to store floating point values
Float is typically referred to as “single
precision”, and takes less space to store
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”
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
Data Type Qualifiers: 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
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
Data Type Qualifiers: signed /
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
Data Type Qualifiers
Unsigned can also be used with short
When using unsigned, signed,
short and long for integer variables,
the int keyword is optional
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
Constants are useful for storing values that
are used repeatedly throughout an application
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
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
Objective-C makes extensive use of pointers, especially for
Pointers are designated with the indirection operator (*)
preceding the variable name in the declaration statement
To get the address of the
you can assign the address of the data object or
scalar variable to an integer variable using the “&”
int anInt = 50;
int *aPointertoAnInt = &anInt;
If you display the value stored in
aPointertoAnInt, you will see the hexadecimal
address of the variable anInt.
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