`

【IOS 开发】Object-C 运算符

 
阅读更多

博客地址 :http://blog.csdn.net/shulianghan/article/details/41624613


1. 算术运算符


算术运算符 : 加(+), 减(-), 乘(*), 除(/), 模(%), 自增(++);

-- 其它运算 : 如果需要平方开放运算, 使用 math.h 中得方法即可;



源码示例 :

/*************************************************************************
    > File Name: 10-arithmetic.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 日 11/30 17:54:01 2014
	算数运算符示例 : 加减乘除
 ************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, char * argv[])
{
	@autoreleasepool {
		// 加法示例
		double a = 3.8;
		double b = 3.8;
		double sum = a + b;
		NSLog(@"加法 : %g", sum);

		//减法示例
		double sub = a - b;
		NSLog(@"减法 : %g", sub);

		//乘法运算
		double multi = a * b;
		NSLog(@"乘法 : %g", multi);

		//除法运算
		double divde = a / b;
		NSLog(@"除法 : divide = %g", divde);
		NSLog(@"除法 : 3 / 0.0 = %g", 3/0.0);
		NSLog(@"除法 : -3/0.0 = %g", -3/0.0);
		
		//取余运算
		int c = 5;
		int d = 2;
		int mod = c % d;
		NSLog(@"取余 : mod = %d", mod);

		//自增运算
		int e = c++ + 6;
		NSLog(@"自增 : c = %d , e = %d", c, e);

		//次方计算, 调用 math.h 中得 pow() 方法进行次方计算;
		double f = pow(a, 2);
		NSLog(@"次方 : f = %g",f);

		//计算平方根
		int g = sqrt(4);
		NSLog(@"平方根 : g = %d", g);

		//获取随机数 5以内的
		int h = arc4random() % 5;
		NSLog(@"随机数 : h = %d", h);
	}
}

执行结果 :

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] 加法 : 7.6
2014-11-30 18:22:24.788 a.out[3198:507] 减法 : 0
2014-11-30 18:22:24.789 a.out[3198:507] 乘法 : 14.44
2014-11-30 18:22:24.790 a.out[3198:507] 除法 : divide = 1
2014-11-30 18:22:24.790 a.out[3198:507] 除法 : 3 / 0.0 = inf
2014-11-30 18:22:24.790 a.out[3198:507] 除法 : -3/0.0 = -inf
2014-11-30 18:22:24.791 a.out[3198:507] 取余 : mod = 1
2014-11-30 18:22:24.792 a.out[3198:507] 自增 : c = 6 , e = 11
2014-11-30 18:22:24.792 a.out[3198:507] 次方 : f = 14.44
2014-11-30 18:22:24.793 a.out[3198:507] 平方根 : g = 2
2014-11-30 18:22:24.793 a.out[3198:507] 随机数 : h = 4




2. 赋值运算符



赋值分析 :

-- OC 字符串赋值 : 将 指针 赋值给 OC 字符串指针, 详情见下面的示例;

-- C 语言字符串赋值 : 将 C 字符串指针, 赋值给 C 字符串指针;

-- 连续赋值 : 多个变量之间连续赋值 a = b = c = 38;


源码示例 :

/*************************************************************************
    > File Name: 10-assignment.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 日 11/30 21:32:18 2014
	赋值运算符示例 = 
 ************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, char * argv[])
{
	@autoreleasepool {
		//将 str 字符串变量 赋值给 str2 字符串变量
		NSString *str = @"octopus";
		//OC 字符串赋值, 直接将指针赋值给 str2 指针
		NSString *str2 = str;
		char * str3 = "han";
		//C 字符串指针赋值 直接将指针赋值给 str4
		char * str4 = str3;
		//打印 Object-C 字符串, 打印时 不带 * 指针符号, 打印 OC 类型使用 %@ 占位符
		NSLog(@"str2 = %@", str2);
		NSLog(@"str4 = %s", str4);

		//连续赋值
		int a, b, c;
		a = b = c = 38;
		NSLog(@"a = %d, b = %d, c = %d", a, b, c);
	}
}

执行结果 :

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 = octopus
2014-12-01 00:44:46.795 a.out[3695:507] str4 = han
2014-12-01 00:44:46.795 a.out[3695:507] a = 38, b = 38, c = 38




3. 位运算符



数据转二进制 :

-- 正数 : 如果数字是正数, 按照正常编码;

-- 负数 : 数字是负数, 除了符号位 1, 最高位不变, 剩下的按位取反 加 一;

-- 负数转成二进制数 : 补码形式 -1, 之后除了最高位之外按位取反;


按位 与 (&) : 有一位是 0, 就是 0;

按位 或 (|) : 有一位是 1, 就是 1;

按位 非 (~) : 所有的值 取 反;

按位 异或 (^) : 不同的取 1, 相同的取 0;

左移 (<<) : 左移, 最低位 补 0, 相当于 乘以 2 ^n;

右移 (>>) : 右移, 最高位 补 符号位, 相当于 除以 2 ^ n;


代码示例 :

/*************************************************************************
    > File Name: 10-bitOperation.m
    > Author: 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 {
		
		// 按位与 01 & 10 = 00
		NSLog(@"1 & 2 = %d", 1 & 2);

		//按位或 01 & 10 = 11
		NSLog(@"1 | 2 = %d", 1 | 2);

		/*
		  按位非 ~ -5
		  源码 10000000 00000000 00000000 00000101
		  反码 11111111 11111111 11111111 11111010 (源码按位取反, 最高位不变)
		  补码 11111111 11111111 11111111 11111011 (补码是反码加一)
		  按位取反结果 100 十进制 4
		 */
		NSLog(@"~ -5 = %d", ~-5);

		//按位异或(相同 0, 不同 1) 011 ^ 110 = 101
		NSLog(@"3 ^ 6 = %d", 3 ^ 6);

		/*
		  5 左移 2 位 101 << 2 结果 10100 16 + 4 = 20
		 */
		 NSLog(@"5 << 2 = %d", 5 << 2);

		 /*
		  -5 左移 2 位
		  -5 补码是 11111111 11111111 11111111 11111011
		  -5 左移后 11111111 11111111 11111111 11101100
		  补码 减一 11111111 11111111 11111111 11101011
		  取反      10000000 00000000 00000000 00010100 结果是 -20
		  */
		  NSLog(@"-5 << 2 = %d", -5 << 2);

		  /*
		    右移, 最高位补符号位
			-5 右移 2 位 
			-5 补码 11111111 11111111 11111111 11111011
			右移2位 11111111 11111111 11111111 11111110
			减一    11111111 11111111 11111111 11111101
			取反    10000000 00000000 00000000 00000010 结果是 -2
		   */
		   NSLog(@"-5 >> 2 = %d", -5 >> 2);

	}
}

执行结果 :

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 = 0
2014-12-01 01:31:47.057 a.out[3813:507] 1 | 2 = 3
2014-12-01 01:31:47.057 a.out[3813:507] ~ -5 = 4
2014-12-01 01:31:47.058 a.out[3813:507] 3 ^ 6 = 5
2014-12-01 01:31:47.058 a.out[3813:507] 5 << 2 = 20
2014-12-01 01:31:47.059 a.out[3813:507] -5 << 2 = -20
2014-12-01 01:31:47.059 a.out[3813:507] -5 >> 2 = -2



4. 比较运算符


比较运算 : 结果是正数, 1 是 真, 0 是 假;

-- == 运算 : 比较数值类型, 即使 类型不同, 值相等, 那么返回 true, 5.0 == 5 结果是 1;

== != 运算 : 比较数值类型, 不管类型, 值不等, 返回 false 0;


源码示例 :

/*************************************************************************
    > 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 {

		//只能进行数值比较, 只进行值的比较
		NSLog(@"3 > 1 = %d", 3 > 1);

		//数据类型不同, 但是如果值 相等, 那么返回 1
		NSLog(@"3 == 3.0 = %d", 3 == 3.0);
		NSLog(@"97 == 'a' = %d", 97 == 'a');

	}
}


执行结果 :

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 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 3 == 3.0 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 97 == 'a' = 1



5. 逻辑运算符


逻辑运算 : 逻辑运算只能是 BOOL 变量或者 常量才能使用该运算符;

-- 与 操作 (&&) : 都为 TRUE 结果才为 TRUE;

-- 或 操作 (||) : 只要有一个是 TRUE, 结果就是 TRUE;

-- 非 操作 (!) : 操作数有一个, !TRUE = FALSE;

-- 异或 操作 (^) : 操作数相同 返回 FALSE, 不用 返回 TRUE;


代码示例 :

/*************************************************************************
    > 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 {
		//非 0 是 TRUE, 非 其它数 是 FALSE
		NSLog(@"!3 = %d", !3);	
		NSLog(@"3 > 2 && '1' > 10 = %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);
	}
}


执行结果 :

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 = 0
2014-12-01 01:54:00.284 a.out[3898:507] 3 > 2 && '1' > 10 = 1
2014-12-01 01:54:00.285 a.out[3898:507] 3 > 2 || 2 > 3 = 1
2014-12-01 01:54:00.286 a.out[3898:507] 3 > 2 ^ 3 > 2 = 0



6. 逗号运算符


逗号表达式 : "表达式1, 表达式2 ... 表达式n", 结果返回的时 表达式n 的值;


代码示例 :

/*************************************************************************
    > 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);
	}
}


执行结果 :

octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-comma.m 
10-comma.m:13:12: warning: expression result unused [-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 = 1
2014-12-01 02:00:06.469 a.out[3919:507] b = 6



7. 三目运算符


三目运算符格式 : "表达式1 : 表达式2 : 表达式3", 如果表达式1 为 true, 返回 表达式2 的值, 表达式1 为 false, 返回 表达式3 的值;


示例源码 :

/*************************************************************************
    > 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是TRUE" : @"a是FALSE" ;	
		NSLog(@"%@", str);
	}
}

执行效果 :

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是FALSE


博客地址:http://blog.csdn.net/shulianghan/article/details/41624613

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics