Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework Homework Continue Reading K&R Chapter 2 Questions?

Similar presentations


Presentation on theme: "Homework Homework Continue Reading K&R Chapter 2 Questions?"— Presentation transcript:

1 Homework Homework Continue Reading K&R Chapter 2 Questions?

2 Converting Decimal to Binary
Divide the decimal number successively by 2 and write down the remainder in binary form: e.g LSB (after divide by 2, remainder =1) (after divide by 2, remainder =0) (after divide by 2, remainder =1) (after divide by 2, remainder =0) (after divide by 2, remainder =1) (after divide by 2, remainder =1) (after divide by 2, remainder =1) MSB Read UP and add any leading 0’s: odd even Quotient

3 Converting Decimal to Hex
Method 1: Convert the decimal number to binary and group the binary digits in groups of 4 e.g   7516 Method 2 (shown in HW3): Divide the decimal number successively by 16 and write down the remainder in hex form: LSB (after divide by 16, remainder =5) MSB (after divide by 16, remainder =7) Read UP and add any leading 0’s: 0x75

4 Converting Binary to Decimal
Treat each bit position n that contains a one as adding 2n to the value. Ignore zeros because they can’t add anything to the value. Bit 0 LSB (= 20) Bit Bit (= 22) Bit (= 23) Bit Bit (= 25) Bit Bit 7 MSB 0 0 Total 45 = (20*1) + (21*0) + (22*1) + (23*1) + (24*0) + (25*1) = 45

5 Converting Hex to Decimal
Treat each digit n as adding 16n to the value. Ignore zeros because they can’t add anything to the value. Digit 0 LSB Digit (= 2 * 161) Digit 2 b (= 11 * 162) Digit Digit Digit (= 1 * 165) Digit Digit 7 MSB Total

6 Converting base x to Decimal
(…abcde)x = (x0*e) + (x1*d) + (x2*c) + (x3*b) + (x4*a) x4 x3 x2 x1 x0

7 Base for Integer Variable
Designating the base for an integer variable If variable begins with either: 0x It is Hex with a-f as Hex digits 0X It is Hex with A-F as Hex digits Otherwise, if variable begins with 0 It is Octal Otherwise, it is decimal Just a convention

8 Base for Character variable
Designating the base for a character variable If variable is like either: ‘\x . . .’ It is Hex with 0-9 and a-f as Hex digits ‘\X . . .’ It is Hex with 0-9 and A-F as Hex digits Otherwise, if variable is like ‘\ ’ It is Octal Otherwise, it is the ASCII code for a character ‘a’

9 Variables Character and Integer variables are Signed!
Sign bit (MSB) not set ‘\x55’ equals an 8-bit quantity when casted to an int, it equals 0000 … 0x55 equals a 32-bit quantity … Sign bit (MSB) set ‘\xaa’ equals an 8-bit quantity when casted to an int, it equals 1111 … 0xaa equals a 32 bit quantity … Note: None of the ASCII codes (x00 - x7f) have sign bit set! Char b; int c; b = 0x55; c = b; All the previous numbers extend to 1

10 Signed (Default) Behavior
Careful mixing modes when initializing variables! int i; (signed behavior is default) char c; (signed behavior is default) i = 0xaa; (== aa) as intended i = ‘\xaa’; (== ffff ffaa) sign extends! c = ‘\xaa’; (== aa) as intended i = c; (==ffff ffaa) sign extends! Sign Bit Extension char 1 int 1

11 Unsigned Behavior Careful mixing modes when initializing variables!
unsigned int i; (must specify unsigned if wanted) unsigned char c; (must specify unsigned if wanted) i = 0xaa; (== aa) as intended i = ‘\xaa’; (== ffffffaa) char sign extends! c = ‘\xaa’; (== aa) as intended i = c; (== aa) char sign not extended! Sign Bit Extension char 1 int 1

12 Example to illustrate signed/unsigned types
void copy_characters(void){ char ch; while ((ch =getchar()) != EOF) putchar(ch); } What happens if ch is defined as unsigned char? Is this true? EOF -> 32 bits 1 If getchar() == EOF, is this true? No yes Changing the declaration of ch into int ch; makes it work.

13 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’ ~ == Number anded with its one’s complement = 0 &

14 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’ == Number added to its two’s complement = 0 (1) (carry out of MSB is dropped) Show an example You can not just flip the MSB and get the negative number. You should get the 2’s complement of the number The computers do the same thing when you want to subtract two numbers

15 Two Special Case Values
char 0 (or zero of any length) = = char -27 (or -2n-1 for any length = n) = = -128 to 127. the number is bigger than you can represent it here. overflow

16 Bit Manipulation Bitwise Operators:
~ one’s complement (unary not) & and | or ^ xor (exclusive or) << left shift >> right shift Lots of possible Exam questions here!

17 Binary Logic Tables AND 1 OR 1 1 NOT 1 1 1 1 1 1 1 XOR 1 ADD 1 1 1 1 1
1 OR 1 1 NOT 1 1 1 1 1 1 1 XOR 1 ADD 1 ^ -> XOR -> Exclusive OR 1 1 Carry 1 1 1 1 1 Operands Results

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

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

20 Bit Manipulations n = ‘\x18’; 00011000 (Remember n is unsigned)
n << shift 1 to left (like times 2) n << shift 2 to left (like times 4) n << shift 4 to left (bits disappear off left end) n >> shift 2 to right (like / 4) n >> shift 4 to right (bits disappear off right end) Shift left -> multiply by 2 Unsigned Right Shift

21 Bit Manipulations Right shift result may be different if n is signed!
If value of n has sign bit = 0, works same as last slide If value of n has sign bit = 1, works differently!! char n = ‘\xa5’; (default is signed) n (sign bit is set) n >> (bring in 1’s from left) Bringing in extra copies of the sign bit on the left is also called "sign extension" Signed Right Shift

22 Bit Manipulations For signed variable, negative value shifted right by 2 or 4 is still a negative value ‘\xa5’ = ‘\xa5’ >> 2 = = ‘\xe9’ Same result as divide by 4 (22 = 4) But, this is not true on all machines See K&R pg. 49, lines 8-10.

23 Bit Manipulations When dividing a negative value Note that (-1)/2 = -1
Different rounding rules than for positive value Remainder must be negative - not positive Note that (-1)/2 = -1 Note that -(1/2) = 0 Show -1 Round down


Download ppt "Homework Homework Continue Reading K&R Chapter 2 Questions?"

Similar presentations


Ads by Google