Symbol Operation
& Bitwise AND
| Bitwise inclusive-OR
^ Bitwise OR
~ Ones complement
<< Left shift
>> Right shift
The Bitwise AND Operator
b1 b2 b1 & b2
———————————0 0 0
0 1 0
1 0 0
1 1 1
w1 0000 0000 0001 0101 0x15
w2 0000 0000 0000 1100 & 0x0c
———————————————————————w3 0000 0000 0000 0100 0x04
The Bitwise Inclusive-OR Operator
b1 b2 b1 | b2
—————————————————
0 0 0
0 1 1
1 0 1
1 1 1
w1 0000 0000 0001 1001 0x19
w2 0000 0000 0110 1010 | 0x6a
————————————————————————————
0000 0000 0111 1011 0x7b
The Bitwise Exclusive-OR Operator ^
b1 b2 b1 ^ b2
———————————————————————
0 0 0
0 1 1
1 0 1
1 1 0
w1 0000 0000 0101 1110 0x5e
w2 0000 0000 1011 0110 ^ 0xd6
————————————————————————————————————
w3 0000 0000 1110 1000 0xe8
The Ones Complement Operator
b1 ~b1
————————
0 1
1 0
w1 1010 0101 0010 1111 0xa52f
~w1 0101 1010 1101 0000 0x5ab0
The Left Shift Operator
w1 ... 0000 0011 0x03
w1 << 1 ... 0000 0110 0x06
The Right Shift Operator
w1 1111 0111 0111 0111 1110 1110 0010 0010 0xF777EE22
w1 >> 1 0111 1011 1011 1011 1111 0111 0001 0001 0x7BBBF711
//bitwiseoperator
#include<Foundation/Foundation.h>
int main(int argc, char*argv[])
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
unsigned int w1=0xA0A0A0A0 ,w2= 0xFFFF0000,w3=0x00007777;
NSLog (@"%x %x %x",w1&w2,w1|w2,w1^w2);
NSLog (@"%x %x %x", ~w1, ~w2, ~w3);
NSLog (@"%x %x %x", w1 ^ w1, w1 & ~w2, w1 | w2 | w3);
NSLog (@"%x %x", w1 | w2 & w3, w1 | w2 & ~w3);
NSLog (@"%x %x", ~(~w1 & ~w2), ~(~w1 | ~w2));
[pool drain];
return 0;}
//output
giripal sigh rawat@SUNIL ~/objc_prog404
$ ./obj/LogTest
2009-08-31 13:44:59.375 LogTest[3080] a0a00000 ffffa0a0 5f5fa0a0
2009-08-31 13:44:59.421 LogTest[3080] 5f5f5f5f ffff ffff8888
2009-08-31 13:44:59.421 LogTest[3080] 0 a0a0 fffff7f7
2009-08-31 13:44:59.421 LogTest[3080] a0a0a0a0 ffffa0a0
2009-08-31 13:44:59.421 LogTest[3080] ffffa0a0 a0a00000

No comments:
Post a Comment