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 PSU ECE
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
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
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 b 0 2 0
4 Binary Numbers Table 1: Given n bits, the number of possible states = 2 n n # States n n 0none101,024201,048, ,048212,097, ,096224,194, ,192238,388, , ,777, ,768:: ,536301,073,741, , ,147,483, , ,294,967, ,
5 Number Conversion Decimal - Binary Convert from binary to its equivalent base 10 value Expand the powers of two Example: What is in decimal? = (1 2 3 ) + (1 2 2 ) + (1 2 1 ) + (0 2 0 ) = (1 8) + (1 4) + (1 2) + (0 1) = Convert from base 10 to its equivalent binary value Successively divide by two; keep track of remainders Example: What is in binary? Base 10Remainder 14 / 2 = 70 7 / 2 = 31 3 / 2 = 11 1 / 2 = 01 Read the remainders backwards. Hence, =
6 Bitwise Operations ABA & B ABA | B ABA ^ B A~A Bitwise AND Bitwise OR Bitwise XOR Bitwise Complement ABA + BCarry Bitwise Addition
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
8 Other Base Representations Octal (base 8 0, …, 7) Hexadecimal (base 16 0, …, 9, A, B, C, D, E, F with F representing ) Table 2: 4-bit positive integer conversion table DecBinOctHexDecBinOctHex A B C D E F
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: = = AC44 16 Converting from hex to its equivalent binary: Replace each hex value by its 4-bit binary value. Example: = 6B13 16 =
10 Positive and Negative Integers Integers are exactly and easily representable in any integer base > 1, including base 2 Base 10Base 2Base 10Base 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
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) 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
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: –634.9 Example: In scientific notation format (base 10) –6.349 × 10 2 mantissa signbase exponent
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 b …+ b if e' ≠ 0 then FP number = (-1) s × (1 + m') × 2 e'-127 if e' = 0 then FP number = (-1) s × m' ×
14 Example: IEEE 754 single precision (32-bit) The more bits available, the more precise the mantissa and the larger the exponent range e ' = s = 0m ' = = Number = (-1) s × (1 + m') × 2 e'-127 = × 2 45 ≈ × 10 13