Homework • Homework – Continue Reading K&R Chapter 2 – We’ll go over HW 2 at end of class today – Continue working on HW 3 • Questions? 1
Forcing Groups of Bits Off • Given char n, how to turn off all bits except the least significant 5 bits: n = n & ‘x 1 f’ n = ‘xa 5’ 10100101 n & ‘x 1 f’ 10100101 & 00011111 turn off all bits 00000101 except bottom 5 • Called "masking" the bits -- only see bits on in result where 1's found in mask value 2
Forcing Groups of Bits Off • x = x & ~077 (octal for a change) Sets least significant 6 bits in x to 0 Even if you don't know size of x (e. g. size of int) ~077 = ~00. . . 00111111 = 11. . . 11000000 of required size Extends itself with 1 bits on left for length of x 3
Forcing Groups of Bits On • Given n, how to turn on the MS two bits (if already on, leave on). n = n | ‘xc 0’ n = 'xa 5' n | 'xc 0': 10100101 | 11000000 turn on MS 2 bits 11100101 4
“Encryption” with Exclusive Or • Show that x ^ (x ^ y) == y char y =‘xa 5’ 10100101 (plain text bits) char x =‘x 69’ 01101001 (encryption key) x ^ y 1100 (cypher text bits) x ^ (x ^ y) 10100101 (decrypted bits) Same as original value of y! 5
Exchanging Data with Exclusive Or • You can use exclusive or to exchange the values of two variables without using a temporary location for a “three way move” int i = 0 x 12, j = 0 x 34; i = i ^ j; /* i = 0 x 26 */ j = i ^ j; /* j = 0 x 12 */ i = i ^ j; /* i = 0 x 34 */ 6
String Constants • String constant: "I am a string. “ – An array (a pointer to a string) of char values somewhere ending with NUL = '