(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.