Objective-C 位运算符
转自:http://www.yiibai.com/objective_c/objective_c_bitwise_operators.html
下表中列出了支持Objective-C语言的位运算符。假设变量A=60和变量B=13,则:
运算符 | 描述说明 | 示例 |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12, which is 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) will give 61, which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49, which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operand's value is moved left by the number of bits specified by the right operand. | A << 2 will give 240, which is 1111 0000 |
>> | Binary Right Shift Operator. The left operand's value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15, which is 0000 1111 |
例子
尝试下面的例子就明白了所有在Objective-C编程语言的位运算符:
- #import <Foundation/Foundation.h>
- main()
- {
- unsigned int a = 60; /* 60 = 0011 1100 */
- unsigned int b = 13; /* 13 = 0000 1101 */
- int c = 0;
- c = a & b; /* 12 = 0000 1100 */
- NSLog(@"Line 1 - Value of c is %d\n", c );
- c = a | b; /* 61 = 0011 1101 */
- NSLog(@"Line 2 - Value of c is %d\n", c );
- c = a ^ b; /* 49 = 0011 0001 */
- NSLog(@"Line 3 - Value of c is %d\n", c );
- c = ~a; /*-61 = 1100 0011 */
- NSLog(@"Line 4 - Value of c is %d\n", c );
- c = a << 2; /* 240 = 1111 0000 */
- NSLog(@"Line 5 - Value of c is %d\n", c );
- c = a >> 2; /* 15 = 0000 1111 */
- NSLog(@"Line 6 - Value of c is %d\n", c );
- }
当编译和执行上述程序,它会产生以下结果:
- 2013-09-07 22:11:51.652 demo[30836] Line 1 - Value of c is 12
- 2013-09-07 22:11:51.652 demo[30836] Line 2 - Value of c is 61
- 2013-09-07 22:11:51.652 demo[30836] Line 3 - Value of c is 49
- 2013-09-07 22:11:51.652 demo[30836] Line 4 - Value of c is -61
- 2013-09-07 22:11:51.652 demo[30836] Line 5 - Value of c is 240
- 2013-09-07 22:11:51.652 demo[30836] Line 6 - Value of c is 15
Objective-C 位运算符的更多相关文章
- java的布尔运算符和位运算符
1.布尔运算符 && 逻辑与: || 逻辑或: != 不等于: 三元操作符:?: :表达式为 condition?expression1:expression2(当条件为真时 ...
- c 语言的位运算符复习
转载和修正,原文连接:http://www.cnblogs.com/911/archive/2008/05/20/1203477.html 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进 ...
- Java的算数运算符、关系运算符、逻辑运算符、位运算符
JAVA的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符 算数运算符(9):+ - * / % ++ -- 关系运算符(6):== != > >= & ...
- c语言基础表达式, 关系运算符, 逻辑运算符, 位运算符, 数据的取值范围, 分支结构(if...else, switch...case)
1.表达式: 表达式的判断是有无结果(值), 最简单的表达式是一个常量或变量, 如:12, a, 3 + 1, a + b, a + 5 都是表达式 2.BOOL(布尔)数据类型: c语言中除了基本数 ...
- PHP中的运算符---位运算符、递增递减运算符、三元运算符、字符串运算符、数组运算符、类型运算符、错误控制运算符
1.位运算符 位运算符用来对整型数的指定位进行置位,如果被操作数是字符串,则对该字符串的ASCII码值进行操作. 运算类型 运算符 举例 结果 按位与 & $a & $b 将$a 与 ...
- ECMAScript 位运算符
位运算符是在数字底层(即表示数字的 32 个数位)进行操作的. 重温整数 ECMAScript 整数有两种类型,即有符号整数(允许用正数和负数)和无符号整数(只允许用正数).在 ECMAScript ...
- javascript运算符——位运算符
× 目录 [1]二进制 [2]非 [3]与[4]或[5]异或[6]左移[7]右移[8]>>>[9]应用 前面的话 位运算符是非常底层的运算,由于其很不直观,所以并不常用.但是,其速度 ...
- PHP中为位运算符(几乎很少用)
PHP语言里的位运算符&.|.^ .~.〈〈 .〉〉 "&" 按位与运算 按位与运算符"&"是双目运算符.其功能是参与运算的两数各对应的 ...
- PHP运算符:算数运算符、逻辑运算符、三目运算符、位运算符、字符串运算符。
赋值运算符 PHP 赋值运算符用于向变量写值. PHP 中基础的赋值运算符是 "=". 这意味着右侧复制表达式会为左侧运算数设置值. _______________________ ...
- Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
位运算符主要针对二进制,它包括了:“与”.“非”.“或”.“异或”.从表面上看似乎有点像逻辑运算符,但逻辑运算符是针对两个关系运算符来进行逻辑运算,而位运算符主要针对两个二进制数的位进行逻辑运算.下面 ...
随机推荐
- LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- APache PDFbox API使用(1)----简单介绍
因为项目的须要.近期在学习APache PDFbox API,Apache PDFbox API是Apache Java 开源社区中个一个项目,其受Apache 版权 V2的保护,其提供了以下的功能 ...
- JetBrains全系列在线激活中心pycharm
题记:有能力还是建议购买正版授权! 01.pycharm下载 https://www.jetbrains.com/pycharm/download/ https://download.jetbrai ...
- MySQL的binlog操作
1. MySQL的binlog有三种模式: statement, row and mixed, 从5.1开始支持row, 默认是row模式 2. 设置参数 # 要配置在mysqld下 [mysqld] ...
- spring aop实现日志收集
概述 使用spring aop 来实现日志的统一收集功能 详细 代码下载:http://www.demodashi.com/demo/10185.html 使用spring aop 来实现日志的统一收 ...
- MySQL与OLAP:分析型SQL查询最佳实践探索
搞点多维分析,糙快猛的解决方式就是使用ROLAP(关系型OLAP)了.数据经维度建模后存储在MySQL,ROLAP引擎(比方开源的Mondrian)负责将OLAP请求转化为SQL语句提交给数据库.OL ...
- shell 脚本启动tomcat服务
#!/bin/bash # kill tomcat进程 tomcat_fashion_dev_pid=`ps aux|grep tomcat_fashion_dev|grep -v "gre ...
- idea上activiti插件的安装及使用
最近做的东西需要用到activiti,做个笔记 首先下载activti插件- actiBPM File - settings-plugins-Browse-Repositories 搜索 actiBP ...
- mvc 模型验证及正则表达式
ASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的. 我们要做的只是给Model类的各属性加 ...
- 【jsp】JSTL标签大全详解
一.JSTL标签介绍 1.什么是JSTL? JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...