Bitwise Operators


**ATTENTION!!**
Before doing this lesson, make sure you truly understand the binary numbering system.If you are not sure, go
here first.

The bitwise operators enable you to directly manipulate the bits inside of bytes. Most programming languages don't allow you to program on such a low level. The bitwise logical operators are as follows:

& Bitwise AND (Must both be true i.e. 1&1 for the result to be true i.e.1)
| Bitwise Inclusive OR (One or the other or both)
^ Bitwise XOR - Exclusive OR (One or the other but not both)
~ Bitwise NOT




To understand what these bitwise operators do, examine the "truth tables" below. In truth tables, 0's are the same as False and 1's the same as True.


AND (&) Operator

Truth table for the AND (&) operator.

1st Bit AND 2nd Bit result
1 & 1 1
1 & 0 0
0 & 1 0
0 & 0 0
Result:

Only True AND True are equal to True.


Now let's see the AND (&) operator in action. If we apply the AND (&) operator to the binary numbers 1011 0111 and 1111 1111 the result is identical to the first binary number. Below we are applying the AND (&) operator to the bits vertically.
	1101 0111   1st byte

	|||| ||||

     &  1111 1111   2nd byte
	---------
	1101 0111   result

The only way to change a bit in the result is if there are zeros in the second byte. A zero in the second byte could operate on a one in the first byte to result in a zero. You can use this to act on bits in a byte to turn ones to zeros - that is you can turn bits off. This can be useful as you will see.

The only difference between the arrangement of bits in the bytes for lowercase letters and the bytes for uppercase letters is the sixth bit. For lowercase letters the sixth bit is 1 or in other words it is On. For uppercase letters this bit is zero or Off. This is shown in the table below for the letter A/a:

letterbit 8bit7bit 6bit 5bit 4 bit 3bit 2bit 1decimal
A0100000165
a0110000197


You can turn off the sixth bit of any byte by applying the AND (&) operator and a byte containing the binary number 1101 1111. Doing this will turn any lowercase letter into an uppercase letter. In C++ you can do this by using the char() function and the decimal equivalent of the binary number 1101 1111 (Hint: 255-32).
Look at this code segment that uses char() for changing a lowercase letter into an uppercase letter by changing the sixth bit of the byte in which the letter is reperesented into a zero.

	char letter;
	cout << "\nPlease enter a lowercase letter: " << flush;
	cin >> letter;
	letter = letter & char(223);
	cout <<  letter;


OR (|) Operator

Truth table for the OR (|) operator.

1st Bit OR 2nd Bit result
1 | 1 1
1 | 0 1
0 | 1 1
0 | 0 0
Result:

Only False with False equal False.


When you examine the result of applying the OR (|) operator to the binary numbers 1101 0111 and 0000 0000 you should notice that you can use bits with a value of 1 to in a byte to turn on bits that have a value of 0 int the other byte.
	1101 0111   1st byte
     |  0000 0000   2nd byte
	---------
	1101 0111   result

You can use a byte containing the binary value of 0010 0000 to turn on the sixth bit in an uppercase character byte to change it into a lowercase letter.

	cin >> letter;
	letter = letter | char(32);
(Note: The OR | key is usually located on the \ backslash key.)

XOR (^) Operator

Truth table for the XOR (^) operator.

1st Bit XOR 2nd Bit result
1 ^ 1 0
1 ^ 0 1
0 ^ 1 1
0 ^ 0 0
Result:

True when one or the other, but not both are true.


When you apply the XOR (^) operator to the two binary numbers such 1011 01111101 0011 and then apply it again to one of the numbers and the result, you will notice someting very interesting.
	1011 0111   1st byte		0110 0100   previous result
     ^  1101 0011   2nd byte	     ^  1101 0011   2nd byte   
	---------			---------
	0110 0100   result		1011 0111   new result-restored 1st byte

In the next few lessons we can use the XOR (^) operator to encrypt texts.


NOT (~) operator

The NOT (~) operator simply turns bits that are on to off, and turns bits that are off to on.
	~ 1010 0011   byte
	  ---------
	  0101 1100   result
You simply apply it to the character in which you want to reverse the bits.

	char letter;
	cin >> letter;
	letter = ~letter;


ASSIGNMENT #6

1.What are the results of the following:
a. 1 ^ 0 & 1 & 1 | 0
b. 1 & 1 & 1 & 1
c.1 ^ 1 ^ 1 ^ 1
d. ~(1 ^ 0)

2.Write a program that takes a word and converts any lowercase letters to uppercase by applying a bit mask (char(223) or char(32)) and a logical operator.

3.Modify the above program so that it also takes an uppercase character and converts it to a lowercase character.

4.Practice writing out the binary numbers from 1 to 128 (0000 0001 to 1000 0000) in order.

Save your source code and executable programs.