Wednesday, July 27, 2011

Lesson 2

Basic data type of Objective-C are

char stores 'A','a','4' occupies in memory 1 Byte
Int stores 10,-234 occupies in memory 4 Bytes
float stores 1.34,-5.66 single precision values, occupies 4 bytes in memory
double stores 1.34,-9.56 double precision values, occupies 8 bytes in memory

Additional types based on Int

short stores 145,-3903 occupies 2 bytes in memory
long stores 343,-1000 occupies 4 bytes in memory
long long stores 1.99999 occupies 8 bytes in memory

other types are

Bool stores 0,1, false, true
void for null
id for object

With exception of both float and double data types, above mentioned all datatypes can be signed or unsigned. if we are not specifying either then compiler will assume signed

int is default datatype if you do not specify type. for example, if you are using signed or unsigned to mean a signed int or unsigned int respectively.

Note that

signed anInt =20 ;

is same as

int anInt =20;

even, you can write signed int anInt=20;

Size Range
1 byte signed: -128 to 127
unsigned: 0 to 255

2 bytes signed: -32768 to 32767
unsigned: 0 to 65535

4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295

8 bytes signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned: 0 to18,446,744,073,709,551,615

For floating point numbers, such as float and double.
For a float, the number of significant digits is 7 or 8, and for a double, the number of significant digits is 15 or 16.

Arithmetic Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo

Example of each operator

int a = 20;
int b = 10;
int c;

c = a + b; //Addition Ans: 30
c = a - b; //Subtraction Ans: 10
c = a * b; //Multiplication Ans: 200
c = a / b; //Division Ans: 2
c = a %b; //Modulo Ans: 0

String format specifiers:

char %c
Int %i,%d
signed int %i, %d
unsigned int %u
short %hi
signed short %hi
unsigned short %hu
long long %qi
signed long %qi
unsigned long %qu
float %f
double %F

Bitwise Operators

In the computer, your data is actually stored as ones and zeros which corresponds to something called a bit. In fact, the basic computations you do are in something called binary arithmetic.

Operator Usage
& Bitwise AND
| Bitwise OR (Bitwise inclusive OR)
^ Bitwise XOR(Bitwise exclusive OR)
~ Unary complement (bit inversion)
<< Shift Left
>> Shift Right

Compound Assignment Operators

+= Addition
-= Subtraction
*= Multiplication
/= Division
%= Modulo
&= Bitwise AND
|= Bitwise inclusive OR
^= Bitwise exclusive OR
<<= Shift Left
>>= Shift Right

Increment and Decrement Operators

++ incremented by 1
-- decremented by 1

for example,

int a = 10;
int b;

b = a++; means the value of b will be 10 and value of a will be 11 as ++ is in suffix mode


b = ++a; means the value of b will be 11 and value of a also will be 11 as ++ is in prefix mode

in prefix mode, first operator increment the value to variable and then assigned to variable

Comma operator

The comma operator (,) allows you to use two or more expressions where only one expression is expected.

int a;
int b;
a = ( b=3, b+2);

the comma operator, in the expression (b=3,b+2) will first evaluate b=3, resulting in the value of b becoming 3. the second operand is then evaluated, adding 2 to b, which results in the comma operator returning 5. Finally a is assigned that result, or 5. So, at the end, variable a will contain the value 5, whereas variable b will contain value 3.

No comments:

Post a Comment