|
Bitwise AND: 00010100 (0 AND 0 = 0 0 AND 1 = 0) 00001111 (1 AND 0 = 0 1 AND 1 = 1) -------- 00000100 |
Operator symbol | Meaning |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
int main( int argc, char* argv[] ) { char a = 20; /* 00010100 = 16 + 4 = 20 */ char b = 15; /* 00001111 = 8 + 4 + 2 + 1 = 15 */ printf( "a & b = %d\n", (a & b) ); printf( "a | b = %d\n", (a | b) ); printf( "a ^ b = %d\n", (a ^ b) ); printf( "~ a = %d\n", (~ a) ); } |
Output: (Note: the "%d" conversion character will output the bit expressions as a decimal number representation)
a & b = 4 ( 4 = 00000100 ) a | b = 31 ( 31 = 00011111 ) a ^ b = 27 ( 27 = 00011011 ) ~ a = -21 ( -21 = 11101011 --- this is 2'c complement code for signed numbers ! ) |
(I have added the convertion of the decimal number into bits between brackets for your viewing convenience)
|
|
|
int main( int argc, char* argv[] ) { char a = 0; /* 0 = 00000000 */ /* Bit pos: 76543210 */ a = a | 4; /* 16 = 00000100 - set 2nd bit */ a = a | 16; /* 16 = 00010000 - set 4th bit */ printf( "a = %d\n", a ); } |
How to run the program:
|
|
|
It is a lot easier to write bit patterns using the Binary Number System
|
Example:
0bxxxxxxxx = the binary number xxxxxxxx Example: 0b00001111 = 15 decimal |
int main( int argc, char* argv[] ) { char a = 0; /* 0 = 00000000 */ /* Bit pos: 76543210 */ a = a | 0b00000100; /* Now: a = 00000100 */ printf( "a = %d\n", a ); a = a | 0b00010000; /* Now: a = 00010100 */ printf( "a = %d\n", a ); } |
How to run the program:
|
|
We can use other related number system to express bit patterns in more compact form
Example:
Representation Value represented --------------------------------------------------- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10 8 11 9 12 10 ... ... 23 2*8 + 3 = 19 |
8 = 23 |
Because the FONT color="blue">Octal number system (8) is related to the Binary number system (2), converting values between these systems is easy (we have discussed this at the start of this course !!!)
|
Example:
0xxxxxxxx = the Octal number xxxxxxxx Example: 011 = 9 decimal (8 + 1) |
int main( int argc, char* argv[] ) { char a = 011; /* 11 Octal = 8 + 1 = 9 decimal */ printf( "a = %d\n", a ); a = 0101; /* 101 Octal = 64 + 1 = 65 decimal */ printf( "a = %d\n", a ); } |
How to run the program:
|
Example:
Representation Value represented --------------------------------------------------- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A 10 B 11 C 12 D 13 E 14 F 15 10 16 11 17 12 18 ... ... 23 2*16 + 3 = 35 |
|
Example:
0xaaaaa = the Hexadecimal number aaaaa Example: 0x1A = 26 decimal (16 + 10) |
int main( int argc, char* argv[] ) { int a = 0x1A; /* 1A Hexadecimal = 16 + 10 = 26 decimal */ printf( "a = %d\n", a ); a = 0x10A; /* 101 Hexadecimal = 256 + 10 = 266 decimal */ printf( "a = %d\n", a ); } |
How to run the program:
|
|
Note:
|
int main( int argc, char* argv[] ) { char a = 31; /* a = 00011111 (= 31) */ a = a & (~16); /* Now: a = 00001111 (= 15) */ printf( "a = %d\n", a ); a = a & (~4); /* Now: a = 00001011 (= 11) */ printf( "a = %d\n", a ); } |
How to run the program:
|
|
|
int main( int argc, char* argv[] ) { char a = 31; /* a = 00011111 (= 31) */ a = a ^ 16; /* Now: a = 00001111 (= 15) */ printf( "a = %d\n", a ); a = a ^ 16; /* Now: a = 00011111 (= 31) */ printf( "a = %d\n", a ); } |
How to run the program:
|
|
int main( int argc, char* argv[] ) { char a = .....; /* Any bit pattern */ if ( (a & 0b00001000) == 0 ) /* Test is 3rd bit is set */ printf( "bit is 0\n" ); else printf( "bit is 1\n" ); } |
How to run the program:
|