Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

2 Syllabus What’s This Blue Code? Binary Numbers Number Conversion Decimal - Binary Bitwise Operations Logic Operations Other Base Representations Convert Decimal to Hex Positive and Negative Integers Floating Point Numbers

3 2 What’s This Blue Code? void foo( void ) { // foo } //end foo // <- implied return here int main( /* no params */ )// better main( void ) { // main foo(); return 0;// says: nothing went wrong! } //end main // learned about: Functions // similar to the printf() which you saw earlier

4 3 Binary Numbers Bit  Smallest unit of information (binary digit) A single bit has one of two distinct states:  0 (logical False, power close to 0 V  1 logical True, power close to defined + V A binary number consists of n bits grouped together. LSBMSB b n-1 b n-2 …b 1 b 0 = b n-1  2 n-1 + b n-2  2 n-2 + … + b 1  2 1 + b 0  2 0

5 4 Binary Numbers Table 1: Given n bits, the number of possible states = 2 n n # States n n 0none101,024201,048,576 12112,048212,097,152 24124,096224,194,304 38138,192238,388,608 4161416,3842416,777,216 5321532,768:: 6641665,536301,073,741,824 71281713,1072312,147,483,648 82561826,2144324,294,967,296 95121952,42886418446744073709551616

6 5 Number Conversion Decimal - Binary Convert from binary to its equivalent base 10 value  Expand the powers of two Example: What is 1110 2 in decimal? 1110 2 = (1  2 3 ) + (1  2 2 ) + (1  2 1 ) + (0  2 0 ) = (1  8) + (1  4) + (1  2) + (0  1) = 14 10 Convert from base 10 to its equivalent binary value  Successively divide by two; keep track of remainders Example: What is 14 10 in binary? Base 10Remainder 14 / 2 = 70 7 / 2 = 31 3 / 2 = 11 1 / 2 = 01 Read the remainders backwards. Hence, 14 10 = 1110 2

7 6 Bitwise Operations ABA & B 000 010 100 111 ABA | B 000 011 101 111 ABA ^ B 000 011 101 110 A~A 01 10 Bitwise AND Bitwise OR Bitwise XOR Bitwise Complement ABA + BCarry 0000 0110 1010 1101 Bitwise Addition

8 7 Logic Operations Logic operations are done one bit at a time (unary, AKA monadic) or a pair of bits (binary, AKA dyadic) Example: ~1011 = 0100Complement, unary 1010 & 1100 = 1000Bitwise AND, binary 1010 | 1100 = 1110Bitwise OR 1010 ^ 1100 = 0110Bitwise XOR

9 8 Other Base Representations Octal (base 8  0, …, 7) Hexadecimal (base 16  0, …, 9, A, B, C, D, E, F with F representing 15 10 ) Table 2: 4-bit positive integer conversion table DecBinOctHexDecBinOctHex 000000081000108 100011191001119 200102210101012A 300113311101113B 401004412110014C 501015513110115D 601106614111016E 701117715111117F

10 9 Convert Binary to Hex Converting from binary to its equivalent hex: 1) Separate binary value into 4-bit groups 2) Replace each group by its hex value Example: 44100 10 = 1010110001000100 2 = AC44 16 Converting from hex to its equivalent binary: Replace each hex value by its 4-bit binary value. Example: 27411 10 = 6B13 16 = 0110101100010011 2

11 10 Positive and Negative Integers Integers are exactly and easily representable in any integer base > 1, including base 2 Base 10Base 2Base 10Base 2 0000081000 1000191001 20010101010 30011111011 40100121100 50101131101 60110141110 70111151111 Table 3: 4-bit positive only values 0 to 15 If only positive integers and zero are needed, then all of the bits in the binary representation are available to express the value. Given : n bits Range: 0 to 2 n – 1

12 11 For negative integers, the most significant bit (MSB) of the binary value is reserved as a sign bit Negative values are expressed as 2’s complement Table 4: 4-bit positive and negative values -8 to +7 Base 10Base 2 (2's comp) Base 10Base 2 (2's comp) 000001111 10001-21110 20010-31101 30011-41100 40100-51011 50101-61010 60110-71001 70111-81000 If both positive and negative integers are needed, the maximum positive value is reduced by a factor of 2 Given : n bits Range: –2 n-1 to 2 n-1 – 1

13 12 Floating Point Numbers God created integers; Man invented floats Floating point is used to express real-valued numbers. There is an implicit base and decimal point Example: 2.0 3.1415 –634.9 Example: In scientific notation format (base 10) –6.349 × 10 2 mantissa signbase exponent

14 13 Binary can be used to represent floating point values, but usually only as an approximation IEEE 754 single-precision (32-bit) standard s e 1 e 2 …e 8 b 1 b 2 …b 23 8 bits Interpreted as unsigned integer e' 1 bit Sign 0 → + 1 → – 23 bits Interpreted as a base 2 value defined as m' = 0.b 1 b 2 …b 23 = b 1 2 -1 + b 2 2 -2 +…+ b 23 2 -23 if e' ≠ 0 then FP number = (-1) s × (1 + m') × 2 e'-127 if e' = 0 then FP number = (-1) s × m' × 2 -126

15 14 Example: IEEE 754 single precision (32-bit) 01010110010010100000000000000000 The more bits available, the more precise the mantissa and the larger the exponent range e ' = 172 10 s = 0m ' = 2 -1 + 2 -4 + 2 -6 = 0.578125 Number = (-1) s × (1 + m') × 2 e'-127 = 1.578125 × 2 45 ≈ 5.55253372027 × 10 13


Download ppt "ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed."

Similar presentations


Ads by Google