The IOS development object-c operator

Source: Internet
Author: User
Tags bitwise operators print object square root

Blog Address : http://blog.csdn.net/shulianghan/article/details/41624613


1. Arithmetic Operators


arithmetic operators : Plus (+), minus (-), multiply (*), except (/), modulo (%), self-increment (+ +);

-- Other operations : if the square open operation is required, the method can be used in math.h;



Source code example :

/************************************************************************* > File name:10-arithmetic.m > Auth Or:octopus > Mail:octopus_truth.163.com > Created time: Day 11/30 17:54:01 2014 example of an arithmetic operator: subtraction *************** /#import <foundation/foundation.h>int Main (int ARGC, char * argv[]) {@autoreleasepool {//addition example Double A = 3.8;double b = 3.8;double sum = a + b; NSLog (@ "addition:%g", sum);//Subtraction Example Double sub = a-B; NSLog (@ "Subtraction:%g", sub);//multiply operation Double multi = A * b; NSLog (@ "multiplication:%g", multi);//division operation Double Divde = A/b; NSLog (@ "Division: Divide =%g", divde); NSLog (@ "Division: 3/0.0 =%g", 3/0.0); NSLog (@ "Division: -3/0.0 =%g", -3/0.0);//take remainder operation int c = 5;int d = 2;int mod = c% d; NSLog (@ "withdraw: MoD =%d", mod);//increment operation int e = c + + + 6; NSLog (@ "Self-increment: c =%d, E =%d", C, E);//The second-party calculation, called the MATH.H in the POW () method for the second-party calculation; double f = pow (A, 2); NSLog (@ "sub-square: F =%g", f);//compute square root int g = sqrt (4); NSLog (@ "square root: G =%d", g);//Gets the random number 5 or less int h = arc4random ()% 5;NSLog (@ "random number: H =%d", h);}} 

Execution Results:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-arithmetic.m octopus-2:oc octopus$./a.out 2014-11-30 18:  22:24.786 a.out[3198:507] Addition: 7.62014-11-30 18:22:24.788 a.out[3198:507] Subtraction: 02014-11-30 18:22:24.789 a.out[3198:507] Multiplication : 14.442014-11-30 18:22:24.790 a.out[3198:507] Division: divide = 12014-11-30 18:22:24.790 a.out[3198:507] Division: 3/0.0 = Inf2 014-11-30 18:22:24.790 a.out[3198:507] Division: -3/0.0 =-inf2014-11-30 18:22:24.791 a.out[3198:507] Remainder: MoD = 12014-11-30 18 : 22:24.792 a.out[3198:507] Self-increment: c = 6, E = 112014-11-30 18:22:24.792 a.out[3198:507] Sub-party: F = 14.442014-11-30 18:22:24.79 3 a.out[3198:507] square root: G = 22014-11-30 18:22:24.793 a.out[3198:507] random number: H = 4




2. Assignment Operators



Assignment Analysis :

-- OC String Assignment : assigns the pointer to the OC string pointer, as shown in the following example;

-- C language String assignment : assigns a C string pointer to the C string pointer;

-- Continuous assignment : Successive assignments between multiple variables a = b = c = 38;


Source code example :

/*************************************************************************    > File name:10-assignment.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created time: Day 11/30 21:32:18 2014 example of an assignment operator =  /#import <foundation/foundation.h  >int Main (int argc, char * argv[]) {@autoreleasepool {//assigns the STR string variable to the STR2 string variable nsstring *str = @ "Octopus";//oc string Assignment, Assign the pointer directly to the STR2 pointer nsstring *str2 = Str;char * str3 = "Han";//c string pointer assignment directly assigns pointer to Str4char * STR4 = str3;//print object-c string, hit printing without * pointer symbol, print OC type using%@ placeholder NSLog (@ "str2 =%@", str2); NSLog (@ "STR4 =%s", STR4);//continuous assignment int A, b, c;a = b = c = 38; NSLog (@ "A =%d, B =%d, c =%d", A, B, c);}}

Execution Results:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-assignment.m octopus-2:oc octopus$ octopus-2:oc octopus$ ./a.out 2014-12-01 00:44:46.793 a.out[3695:507] str2 = octopus2014-12-01 00:44:46.795 a.out[3695:507] Str4 = han2014-12-0 1 00:44:46.795 a.out[3695:507] A = 38, B =.




3. Bitwise operators



data transfer binary:

- Positive : If the number is positive, follow the normal code;

negative numbers: The number is negative, in addition to the sign bit 1, the highest bit unchanged, the rest of the bitwise take back plus one;

-- negative numbers turn into binary numbers : complement form-1, followed by bitwise negation except for the highest bit;


Bitwise and (&) : One is 0, that is 0;

Bitwise OR (|) : One is 1, that is 1;

bitwise NON (~) : all values are reversed;

Bitwise XOR (^) : Different fetch 1, same fetch 0;

shift Left (<<) : Shift left, minimum bit 0, equal to 2 ^n;

shift Right (>>) : Move right, the highest bit fill symbol, equal to 2 ^ n;


code example :

/************************************************************************* > File name:10-bitoperation.m > Au Thor:octopus > Mail:octopus_truth.163.com > Created Time: 12/1 00:52:53 2014 *************************** /#import <foundation/foundation.h>int Main (int argc, char * argv[  ] {@autoreleasepool {//bitwise vs. & = 00NSLog (@ "1 & 2 =%d", 1 & 2),//bitwise-OR-11NSLog (@ "1 | 2 =%d", 1 | 2);/* Bitwise NON ~-5 source 10000000 00000000 00000000 00000101 Anti-code 11111111 11111111 11111111 11111010 (source bitwise reversed, highest bit unchanged) complement 111111 11 11111111 11111111 11111011 (complement is anti-code plus one) bitwise inverse result 1000 binary 4 */nslog (@ "~-5 =%d", ~-5);//Bitwise XOR (same 0, different 1) 011 ^ 101NSL OG (@ "3 ^ 6 =%d", 3 ^ 6);/* 5 shift left 2-bit 101 << 2 Results 10100 + 4 = */NSLog (@ "5 << 2 =%d", 5 << 2); /*-5 Shift left 2-5 complement is 11111111 11111111 11111111 11111011-5 left shift 11111111 11111111 11111111 11101100 complement minus 111,111,111 1111 1111 11111111 11101011 Take counter     10000000 00000000 00000000 00010100 The result is -20 */NSLog (@ "-5 << 2 =%d", 5 << 2);  /* Shift right, maximum bit-5 to right 2-5 complement 11111111 11111111 11111111 11111011 right shift 2 bit 11111111 11111111 11111111 11111110 minus 111,111,111 11111111 11111111 11111101 Inverse 10000000 00000000 00000000 00000010 The result is -2 */NSLog (@ "-5 >> 2 =%d", 5 >&gt ; 2);}}

Execution Result :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-bitoperation.m octopus-2:oc octopus$ octopus-2:oc octopus$./a.out 2014-12-01 01:31:47.055 a.out[3813:507] 1 & 2 = 02014-12-01 01:31:47.057 a.out[3813:507] 1 | 2 = 32014-12-01 01:31:47.057 a.out[3813:507] ~-5 = 42014-12-01 01:31:47.058 a.out[3813:507] 3 ^ 6 = 52014-12-01 01:31:47. 058 a.out[3813:507] 5 << 2 = 202014-12-01 01:31:47.059 a.out[3813:507]-5 << 2 = -202014-12-01 01:31:47.059 A. OUT[3813:507]-5 >> 2 =-2



4. Comparison Operators


comparison Operation : The result is positive, 1 is true, 0 is false;

--= = Arithmetic : Compare numeric types, even if the types are different, the values are equal, then return true, 5.0 = = 5 the result is 1;

= = = Arithmetic : Compares numeric types, regardless of type, with unequal values, and returns false 0;


Source code example :

/*************************************************************************    > File name:10-compare.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 12/1 01:35:57 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {///Only numeric comparisons are performed, only values are compared NSLog (@ "3 > 1 =%d", 3 > 1);//data types are different, but if the values are equal, return 1NSLog (@ "3 = = 3.0 =%d ", 3 = = 3.0); NSLog (@ "+ = = ' a ' =%d", + = ' a ');}}


Execution Result :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-compare.m octopus-2:oc octopus$./a.out 2014-12-01 01:44: 56.099 a.out[3844:507] 3 > 1 = 12014-12-01 01:44:56.101 a.out[3844:507] 3 = = 3.0 = 12014-12-01 01:44:56.101 a.out[3844: 507] + = = ' a ' = 1



5. Logical Operators


logical Operation : The logical operation can only be a BOOL variable or constant in order to use the operator;

-- with Operation (&&) : True results are true;

-- or action (| |) : As long as one is true, the result is true;

-- non-operational (!) : Operand has one,! TRUE = FALSE;

-- xor operation (^) : The operand returns FALSE without returning TRUE;


code example :

/*************************************************************************    > File name:10-logic.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 12/1 01:49:28 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {//Non 0 is TRUE, non-other number is Falsenslog (@ "!3 =%d",! 3); NSLog (@ "3 > 2 && ' 1 ' > ten =%d", 3 > 2 && ' 1 ' > 10); NSLog (@ "3 > 2 | | 2 > 3 =%d", 3 > 2 | | 2 > 3); NSLog (@ "3 > 2 ^ 3 > 2 =%d", 3 > 2 ^ 3 > 2);}}


Execution Result :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-logic.m octopus-2:oc octopus$./a.out 2014-12-01 01:54:00 .282 a.out[3898:507]!3 = 02014-12-01 01:54:00.284 a.out[3898:507] 3 > 2 && ' 1 ' > 10 = 12014-12-01 01:54:00. 285 a.out[3898:507] 3 > 2 | | 2 > 3 = 12014-12-01 01:54:00.286 a.out[3898:507] 3 > 2 ^ 3 > 2 = 0



6. Comma operator


comma-expression : "Expression 1, expression 2 ... Expression n ", the result returns the value of the expression N;


code example :

/*************************************************************************    > File name:10-comma.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 12/1 01:57:31 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {int a = (3, 3 > 2); NSLog (@ "a =%d", a), int b = (A = 3, a = 4, a = 5, a = 6); NSLog (@ "B =%d", b);}}


Execution Result :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-comma.m 10-comma.m:13:12:warning:expression result Unus Ed [-wunused-value]                int a = (3, 3 > 2);                         ^1 warning Generated.octopus-2:oc octopus$./a.out 2014-12-01 02:00:06.466 a.out[3919:507] A = 12014-12-01 02:00:06.469 A. OUT[3919:507] B = 6



7. Three mesh operator


three mesh operator format : "Expression 1: Expression 2: Expression 3", if expression 1 is true, return the value of expression 2, expression 1 is false, return the value of expression 3;


Sample source code :

/*************************************************************************    > File name:10-three.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created Time: 12/1 02:03:40 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {int a = 0; NSString *str = a? @ "A is true": @ "A is false"; NSLog (@ "%@", str);}}

Execution Effect:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 10-three.m octopus-2:oc octopus$./a.out 2014-12-01 02:05:42 .389 a.out[3930:507] A is false


Blog Address : http://blog.csdn.net/shulianghan/article/details/41624613

The IOS development object-c operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.