在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
转自:http://www.yiibai.com/objective_c/objective_c_bitwise_operators.html 下表中列出了支持Objective-C语言的位运算符。假设变量A=60和变量B=13,则:
例子尝试下面的例子就明白了所有在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 |
请发表评论