1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
2 Base for Integer Constants Designating the base for an integer constant If constant begins with either: 0x It is Hex with a-f as Hex digits 0XIt is Hex with A-F as Hex digits Otherwise, if constant begins with 0It is Octal Otherwise, it is decimal 23Decimal 23
3 Base for Character Constants Designating the base for a character constant If constant begins with either: ‘\x It is Hex with a-f as Hex digits ‘\XIt is Hex with A-F as Hex digits Otherwise, if constant begins with ‘\0It is Octal Otherwise, it is the ASCII code for a character ‘a’ASCII code for lower case character a
4 Signed/Unsigned Constants Constants are Signed by default! Important when converted to 32 bits of significance –When sign bit (MSB) is equal to 0: 0x55 as a 32 bit quantity 0000 … ‘\x55’ as a 32 bit quantity 0000 … When sign bit (MSB) is equal to 1: 0xaa as a 32 bit quantity 0000 … ‘\xaa’ as a 32 bit quantity 1111 …
5 What we’d expect to see! C Source Code int i = 0x55; /* int set = to int constant */ int j = '\x55'; /* int set = to char constant */ printf("i= %x, j= %x\n", i, j); Executes and prints as i= 55, j= 55
6 What Happened? C Source code int i = 0xaa;/* int set = to int constant */ int j = '\xaa'; /* int set = to char constant */ printf("i= %x, j= %x\n", i, j); Executes and prints: i= aa, j= ffffffaa
7 Signed (Default) Behavior Careful mixing modes when initializing variables! int i = 0xaa; (= aa) probably as intended int j = ‘\xaa’;(= ff ff ff aa) sign bit extension! char int Sign Bit Extension char int Sign Bit Extension
8 Signed (Default) Behavior Careful mixing modes when initializing variables! c = 0x ;(= ‘\x89’) truncates MSBs char int NOTE: char value is negative even though int was positive +
9 Unsigned Behavior C Source Code unsigned int i = 0xaa; unsigned int j = '\xaa'; printf("i= %x, j= %x\n", i, j); Executes and prints as: i= aa, j= aa
10 One’s Complement Two’s Complement One’s Complement Character Arithmetic ~ is the one’s complement operator ~ flips the value of each bit in the data All zeroes become one, All ones become zero ~’\xaa’ == ‘\x55’ ~ ==
11 One’s Complement Two’s Complement Two’s Complement Character Arithmetic - is the two’s complement operator -flips the value of each bit in the data and adds 1 It creates the negative of the data value - ‘\x55’ == ‘\xab’ ==
12 Two Special Case Values char 0 (or zero of any length) = = char -2 7 (or -2 n-1 for any length = n) = =
13 Bit Manipulation Bitwise Operators: ~ unary not & and | or ^ xor (exclusive or) << leftshift >> rightshift Operate on pair of bits of the input data values!
14 Binary Logic Tables & ~ | Carry ^ NOT ANDOR XORADD
15 Bit Manipulation unsigned char n = ‘\xa6’; n ~n (1s complement: flip bits) n | ‘\x65’ turn on bit in result if | on in either operand
16 Bit Manipulations n & ‘\x65’ turn on bit in result if & on in both operands n ^ ‘\x65’ turn on bit in result if ^ on in exactly 1 operand
17 Bit Manipulations (Left Shift) char n = ‘\x18’; n << shift 1 to left (like * 2) n << shift 2 to left (like * 4) (MSBs disappear off left end) n can be either signed or unsigned – no difference Significance can be lost if data shifted too far left Left Shift0
18 Bit Manipulations (Right Shift) unsigned char n = ‘\x18’; n >> shift 1 to right (like / 2) n >> shift 2 to right (like / 4) (LSBs disappear off right end) Integer truncation of divide by 2 n just like with / Unsigned Right Shift 0
19 Bit Manipulations (Right Shift) signed char n = ‘\xa5’; n >> (bring in a 1 from the left) n >> (bring in two 1’s from left) The rule is to bring in a copy of the sign bit! Bringing in extra copies of the sign bit from the left end is another form of "sign extension" Signed Right Shift
20 Bit Manipulations (Right Shift) For a signed variable, negative value shifted right by 2 is still a negative value ‘\xa5’ == ‘\xa5’ >> 2 == == ‘\xe9’ Same result as divide by 4 (2 2 = 4)
21 Bit Manipulations (Right Shift) When right shifting a signed value: Different rounding results for positive vs negative Result stays negative – does not become positive 1>>1 = 0 Rounds down -(1>>1) = 0 Rounds down then applies minus (-1)>>1 = -1Applies minus then rounds down -1>>1 = -1Applies minus then rounds down Rounding behavior not exactly same as /2
22 Converting Decimal to Binary From get binary by halving the number successively (rounding down) and writing down the digit 1 when have an odd result, 0 when result is even (consider number itself the first result): 1171 LSB MSB Read UP and add any leading 0’s: = ‘\x75’
23 Unsigned Binary to Decimal Treat each bit position n that contains a one as adding 2 n to the value. Ignore zeros because they can’t add anything to the value. Bit 0LSB 1 1(= 2 0 ) Bit 10 0 Bit 21 4(= 2 2 ) Bit 31 8(= 2 3 ) Bit 40 0 Bit 5132(= 2 5 ) Bit 60 0 Bit 7MSB1 128(= 2 7 ) Total 173
24 Signed Binary to Decimal Treat each bit position n that contains a one as adding 2 n to the value except treat the MSB as subtracting 2 n. (Ignore zeros as before) Bit 0LSB 1 1(= 2 0 ) Bit 10 0 Bit 21 4(= 2 2 ) Bit 31 8(= 2 3 ) Bit 40 0 Bit 5132(= 2 5 ) Bit 60 0 Bit 7MSB1 -128(= -2 7 ) Total -83
25 Converting Hex to Decimal Treat each digit n as adding 16 n to the value. Ignore zeros because they can’t add anything to the value. Digit 0LSBs 0 0 Digit (= 2 * 16 1 ) Digit 2b 2816 (= 11 * 16 2 ) Digit 30 0 Digit 40 0 Digit (= 1 * 16 5 ) Digit 60 0 Digit 7MSBs0 0 Total