09 - Data Types - Engineering 101 Quote of the Day Judge...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Judge people by their questions rather than by their answers. - Voltaire Data Types Two Important Halves of Computer Programming Execution Control Sequence Selection Iteration Data Representation All data inside the computer is represented by bits. Bits can have only two values 0 or 1. 4 bits make a nibble 8 bits make a byte 11001010 10101011 Data Representation How do we structure the data that we are acting on? Data Encoding Bytes 11001110 10101010 11110000 10100101 11111111 11001110 11001110 How many things can you encode with a byte (8 bits)? Each bit allows two possibilities. 1 bit: 0 1 (2 things) 2 bits: 00 01 10 11 (4 things) (8 things) 3 bits: 000 001 010 011 100 101 110 111 n bits: 2n things 8 bits: 256 things Meaning Hail! to the victors valiant Hail! Hail! 1 Base 2 is a number system like base 10 456,347 Base 2 is a number system like base 10 456,347 100000 105 4 10000 104 5 1000 103 6 100 102 3 10 101 4 1 100 7 Base 2 is a number system like base 10 456,347 100000 105 4 10000 104 5 1000 103 6 100 102 3 10 101 4 1 100 7 Base 2 is a number system like base 10 110001 32 25 1 16 24 1 8 23 0 4 22 0 2 21 0 1 20 1 4 x 105 + 5 x 104 + 6 x 103 + 3 x 102 + 4 x 101 + 7 x 100 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 Base 2 is a number system like base 10 110001 32 25 1 16 24 1 8 23 0 4 22 0 2 21 0 1 20 1 Convert the Binary Number to Decimal 128 27 64 26 32 25 16 24 8 23 4 22 2 21 1 20 01011001 1- 27 2- 89 3- 67 4- 79 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 110001(base 2) = 32+16+1 = 49(base 10) 2 Convert the Binary Number to Decimal 128 27 64 26 32 25 16 24 8 23 4 22 2 21 1 20 Adding Binary Numbers 01100 01110 01101011 1- 85 2- 101 3- 97 4- 107 + Adding Binary Numbers 01100 01110 0 Adding Binary Numbers 01100 01110 10 + numbers are added column by column + numbers are added column by column Adding Binary Numbers 01100 01110 210 Adding Binary Numbers 1 01100 01110 010 + 2 is not a valid binary digit. In this case recall that 10 in binary equals 2 in decimal + So we replace the 2 by a 0 and carry the extra 1. 3 Adding Binary Numbers 1 01100 01110 010 Adding Binary Numbers 11 01100 01110 1010 + Now what do we do with 1 + 1 + 1? Recall that 11 in binary equals 3 in decimal + So we output a 1 and carry the extra 1. Adding Binary Numbers 11 01100 01110 11010 (8 + 4 = 12) (8 + 4 + 2 = 14) (16 + 8 + 2 = 26) Add the Binary Numbers 0101 + 0010 = 1- 0110 2- 0111 3- 1001 4- 1010 + Add the Binary Numbers 0101 + 0101 = 1- 0110 2- 0111 3- 1001 4- 1010 Overflow in Binary Addition Typically we are limited to representing a number by a certain number of binary digits. Let's say we are using 4 bits and want to add 1110 and 0010 The answer is 10000, but we only have 4 bits so the extra 1 is lost and we reset to 0000 This is like the odometer on your car. Such overflow can lead to funny unexpected results. 4 Subtracting Binary Numbers 01100 00110 0 Subtracting Binary Numbers 0 0 1 1 10 0 00110 0 - Again we subtract column by column - But we may have to carry like we would in decimal subtraction. Subtracting Binary Numbers 0 01120 00110 0 Subtracting Binary Numbers 0 01120 00110 10 - But we may have to carry much as we would in decimal subtraction. - But we may have to carry much as we would in decimal subtraction. Subtracting Binary Numbers 02 01120 00110 110 Subtracting Binary Numbers 02 01120 00110 00110 - But we may have to carry much as we would in decimal subtraction. - But we may have to carry much as we would in decimal subtraction. 5 Subtract the Binary Numbers 0101 0010 = 1- 0010 2- 0011 3- 0110 4- 1001 Subtract the Binary Numbers 1000 0010 = 1- 0010 2- 0011 3- 0110 4- 1001 Types Types provide an encoding. Types also provide a set of operations that can be performed on this encoding. So far we have used two types: int and double Integers Integers can be converted from base 2 Negative integers are more complex 01000001 01000010 01000011 01100001 01100010 01100011 00110001 65 66 67 97 98 99 49 Negative Binary Numbers How do we encode negative numbers in binary? Consider our 4 bit overflow problem. 1111 + 0001 = 0000 Also, 0000 0001 = 1111 Negative Binary Numbers (32 bit, 2's-Complement) 2's00000000000000000000000000000100 = 4 00000000000000000000000000000011 = 3 00000000000000000000000000000010 = 2 00000000000000000000000000000001 = 1 00000000000000000000000000000000 = 0 11111111111111111111111111111111 = -1 11111111111111111111111111111110 = -2 11111111111111111111111111111101 = -3 11111111111111111111111111111100 = -4 You can find the negative of a number by flipping all the bits and adding 1. This implies that 1111 in our 4 bit representation could stand for -1 6 Other Integer Types int standard integer 4 bytes -2147483648 to 2147483647 char type holds individual characters Characters can be converted to integers. We denote char literals by enclosing them in single quotes like: `F' There are a number of whitespace characters to denote spaces ` ', tabs `\t', newlines `\n', etc. ', t', n', unsigned int cannot be negative 4 bytes 0 to 4294967295 char bool a character: `a', `D' 1 byte true or false 1 byte Reading and Writing char's Characters can be read in from input streams and written to output streams char c; cin >> c; cout << c << endl; Usually whitespace char's are ignored during input unless you specifically turn this feature off. cin.unsetf(ios::skipws); Next Lecture Static Variables / Scope / Lifetime 7 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.

Ask a homework question - tutors are online