286 Chapter 12 Operations on Bits
w1gets ANDed with the correct value on any machine because the ones complement of
1 is calculated and consists of as many leftmost one bits as are necessary to fill the size of
an int(31 leftmost bits on a 32-bit integer system).
Program 12.2 summarizes the various bitwise operators presented thus far. Before
proceeding, however, it is important to mention the precedences of the various opera-
tors.The AND, OR, and Exclusive-OR operators each have lower precedence than any
of the arithmetic or relational operators, but higher precedence than the logical AND
and logical OR operators.The bitwise AND is higher in precedence than the bitwise
Exclusive-OR, which in turn is higher in precedence than the bitwise OR.The unary
ones complement operator has higher precedence than anybinary operator. For a sum-
mary of these operator precedences, see Appendix A, “C Language Summary.”
Program 12.2 Illustrate Bitwise Operators
/* Program to illustrate bitwise operators */
#include <stdio.h>
int main (void)
{
unsigned int w1 = 0525u, w2 = 0707u, w3 = 0122u;
printf ("%o %o %o\n", w1 & w2, w1 | w2, w1 ^ w2);
printf ("%o %o %o\n", ~w1, ~w2, ~w3);
printf ("%o %o %o\n", w1 ^ w1, w1 & ~w2, w1 | w2 | w3);
printf ("%o %o\n", w1 | w2 & w3, w1 | w2 & ~w3);
printf ("%o %o\n", ~(~w1 & ~w2), ~(~w1 | ~w2));
w1 ^= w2;
w2 ^= w1;
w1 ^= w2;
printf ("w1 = %o, w2 = %o\n", w1, w2);
return 0;
}
Program 12.2 Output
505 727 222
37777777252 37777777070 37777777655
0 20 727
527 725
727 505
w1 = 707, w2 = 525