Bitwise operations can be performed on either integers or chars.
The << operator shifts bits n bits to the left and the >> operator shifts bits n bits to the right. Here are several examples assuming that integers are 8 bit quantities:
i = 6 // i = 00000110 i = i << 3 // i = 00110000 (48 in base 10) i = 86 // i = 01010110 i = i << 3 // i = 10110000 (176 in base 10)  leftmost 3 bits thrown away i = 86 // i = 01010110 i = i >> 3 // i = 00001010 (10 in base 10)  rightmost 3 bits thrown away i = 6 // i = 00000110 i = i >> 3 // i = 00000000  rightmost 3 bits thrown awayNotice that the left shift operator throws away the leftmost three bits and adds three 0 bits at the end of the string. Similarly, the right shift operator "throws away" the rightmost three bits and adds three 0 bits to the beginning of the string. In general, left shifting by n bits throws away the leftmost n bits and adds n 0 bits to the right side of the string (thus multiplying the string by 2^{n}). Similarly, right shifting by n bits throws away the rightmost n bits and adds n 0 bits to the left side of the string (thus dividing the string by 2^{n}).
The bitwise logic operators perform bitwise logic operations on each pair of bits in the two operands. There are three bitwise logic operators:
The truth tables for these three operators are as follows:



Here are some examples:
0110  1010 = 1110 0110 & 1010 = 0010 0110 ^ 1010 = 1100To see how each of these results get computed, put the second operand under the first, like you might do in an arithmetic operation:
0110  1010  1110 0110 & 1010  0010 0110 ^ 1010  1100
In a real program, you might actually write:
int a = 6; // a = 0110 int b = 10; // b = 1010 int c = a  b; // c = 1110 (14 in base 10)
You will see that there are many uses for bit manipulation operations as you proceed through your computer science courses. Here are a few uses in this course:
It is tiresome to have to write out binary strings and so computer scientists often write out hexadecimal strings instead. If you remember from COSC 130, hexadecimal is base 16. Here are the conversion tables in case you have forgotten them:
binary (base 2)  decimal (base 10)  hexadecimal (base 16) 

0000  0  0 
0001  1  1 
0010  2  2 
0011  3  3 
0100  4  4 
0101  5  5 
0110  6  6 
0111  7  7 
1000  8  8 
1001  9  9 
1010  10  A 
1011  11  B 
1100  12  C 
1101  13  D 
1110  14  E 
1111  15  F 
We can replace each string of 4 binary digits with a single hexadecimal digit. For example:
0100 1100 1010 0100 1111 0010 1011 0111 = 0x4CA4F2B7We typically write 0x in front of a hexadecimal string to make it clear that the string is a hexadecimal number. You can get printf to print a number in hexadecimal format using the %x directive. For example:
unsigned int x = 14; unsigned int y = 2593 printf("x = %x\n", x); printf("y = %x\n", y);will print
x = e y = a21You should become proficient both at converting binary strings to hexadecimal strings and hexadecimal strings to binary strings. Notice that printf does not put 0x in front of the result, so more typically you would put a 0x in front of the formatting directive as follows:
unsigned int x = 14; unsigned int y = 2593 printf("x = 0x%x\n", x); printf("y = 0x%x\n", y);will print
x = 0xe y = 0xa21