Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Homework Turn in HW2 tonight HW3 is on-line already Questions?

Similar presentations


Presentation on theme: "1 Homework Turn in HW2 tonight HW3 is on-line already Questions?"— Presentation transcript:

1 1 Homework Turn in HW2 tonight HW3 is on-line already Questions?

2 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 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 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 … 01010101 ‘\x55’ as a 32 bit quantity 0000 … 01010101 When sign bit (MSB) is equal to 1: 0xaa as a 32 bit quantity 0000 … 10101010 ‘\xaa’ as a 32 bit quantity 1111 … 10101010

5 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 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 7 Signed (Default) Behavior Careful mixing modes when initializing variables! int i = 0xaa; (= 000000aa) probably as intended int j = ‘\xaa’;(= ff ff ff aa) sign bit extension! char int Sign Bit Extension 0 000000000 char int 1 11111111 1 Sign Bit Extension

8 8 Signed (Default) Behavior Careful mixing modes when initializing variables! c = 0x23456789;(= ‘\x89’) truncates MSBs char int00100011010001010110011110001001 - NOTE: char value is negative even though int was positive +

9 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 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’ ~10101010 == 01010101

11 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’ - 01010101 == 10101011

12 12 Two Special Case Values char 0 (or zero of any length) -00000000 = 11111111 + 1 = 00000000 char -2 7 (or -2 n-1 for any length = n) -10000000 = 01111111 + 1 = 10000000

13 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 14 Binary Logic Tables &01 0 11 0 0 0 ~ 0 0 1 1 |01 0 11 0 1 1 +01 0 1 0 Carry 1 0 1 1 ^01 0 1 10 01 NOT ANDOR XORADD

15 15 Bit Manipulation unsigned char n = ‘\xa6’; n 10100110 ~n 01011001 (1s complement: flip bits) n | ‘\x65’ 10100110 turn on bit in result if | 01100101 on in either operand 11100111

16 16 Bit Manipulations n & ‘\x65’10100110 turn on bit in result if &01100101 on in both operands 00100100 n ^ ‘\x65’10100110 turn on bit in result if ^01100101 on in exactly 1 operand 11000011

17 17 Bit Manipulations (Left Shift) char n = ‘\x18’; 00011000 n << 100110000shift 1 to left (like * 2) n << 201100000shift 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 18 Bit Manipulations (Right Shift) unsigned char n = ‘\x18’;00011000 n >> 100001100shift 1 to right (like / 2) n >> 200000110shift 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 19 Bit Manipulations (Right Shift) signed char n = ‘\xa5’; 10100101 n >>1 11010010 (bring in a 1 from the left) n >>2 11101001 (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 20 Bit Manipulations (Right Shift) For a signed variable, negative value shifted right by 2 is still a negative value ‘\xa5’ == 10100101 ‘\xa5’ >> 2 == 11101001 == ‘\xe9’ Same result as divide by 4 (2 2 = 4)

21 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 22 Converting Decimal to Binary From 117 10 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 580 291 140 71 31 11 00 MSB Read UP and add any leading 0’s: 01110101 = ‘\x75’

23 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 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 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 12 32 (= 2 * 16 1 ) Digit 2b 2816 (= 11 * 16 2 ) Digit 30 0 Digit 40 0 Digit 511048576 (= 1 * 16 5 ) Digit 60 0 Digit 7MSBs0 0 Total1051424


Download ppt "1 Homework Turn in HW2 tonight HW3 is on-line already Questions?"

Similar presentations


Ads by Google