ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU Status 6/20/2016 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE
Syllabus Binary Numbers Bitwise Operations Other Base Representations Positive and Negative Integers Floating Point
Binary Numbers Bit Smallest unit of information (binary digit) A single bit has two distinct states: 0 (logical False) 1 (logical True) A binary number consists of n bits grouped together. LSB MSB bn-1bn-2…b1b0 = bn-12n-1 + bn-22n-2 + … + b121 + b020 2
Table 1: Given n bits, the number of possible states = 2n - 10 1024 20 1048576 1 2 11 2048 21 2097152 4 12 4096 22 4194304 3 8 13 8192 23 8388608 16 14 16384 24 16777216 5 32 15 32768 : 6 64 65536 30 1073741824 7 128 17 131072 31 2147483648 256 18 262144 4294967296 9 512 19 524288 18446744073709551616 3
Convert from binary to its equivalent base 10 value Expand the powers of two. Example: What is 11102 in decimal? 11102 = (123) + (122) + (121) + (020) = (18) + (14) + (12) + (01) = 1410 Convert from base 10 to its equivalent binary value Successively divide by two; keep track of remainders. Example: What is 1410 in binary? Base 10 Remainder 14 / 2 = 7 7 / 2 = 3 1 3 / 2 = 1 1 / 2 = 0 Read the remainders backwards. Hence, 1410 = 11102 4
Bitwise Operations 1 1 1 1 1 A B A & B A B A | B A B A ^ B A ~A 1 A B A | B 1 A B A ^ B 1 A ~A 1 Bitwise Complement Bitwise AND Bitwise OR Bitwise XOR A B A + B Carry 1 Bitwise Addition 5
Logic operations are done a bit at a time (unary operator) or a pair of bits at a time (binary operator). Example: ~1011 = 0100 Complement 1010 & 1100 = 1000 Bitwise AND 1010 | 1100 = 1110 Bitwise OR 1010 ^ 1100 = 0110 Bitwise XOR ■ ■ ■ ■ 6
Table 2: 4-bit positive integer conversion table Other Base Representations Octal (base 8 0, …, 7) Hexadecimal (base 16 0, …, 9, A, B, C, D, E, F) Table 2: 4-bit positive integer conversion table Dec Bin Oct Hex 0000 8 1000 10 1 0001 9 1001 11 2 0010 1010 12 A 3 0011 1011 13 B 4 0100 1100 14 C 5 0101 1101 15 D 6 0110 1110 16 E 7 0111 1111 17 F 7
Converting from binary to its equivalent hex: 1) Separate binary value into 4-bit groups 2) Replace each group by its hex value Example: 4410010 = 10101100010001002 = AC4416 Converting from hex to its equivalent binary: Replace each hex value by its 4-bit binary value. 2741110 = 6B1316 = 01101011000100112 8
Positive and Negative Integer Numbers Integers are exactly representable in base 2 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 2n – 1 Base 10 Base 2 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111 9
Table 4: 4-bit positive and negative values -8 to +7 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 If both positive and negative integers are needed, the maximum positive value is reduced by a factor of two. Given : n bits Range: –2n-1 to 2n-1 – 1 Base 10 Base 2 (2's comp) 0000 -1 1111 1 0001 -2 1110 2 0010 -3 1101 3 0011 -4 1100 4 0100 -5 1011 5 0101 -6 1010 6 0110 -7 1001 7 0111 -8 1000 10
Floating Point Numbers Floating point is used to express real-valued numbers. There is an implicit decimal point. Example: 2.0 3.1415 –634.9 Example: In scientific notation format (base 10) –6.349 × 102 mantissa sign base exponent 11
IEEE 754 single-precision (32-bit) standard s e1e2…e8 b1b2…b23 Binary can be used to represent floating point values, but usually only as an approximation. IEEE 754 single-precision (32-bit) standard s e1e2…e8 b1b2…b23 1 bit Sign 0→ + 1→ – 8 bits Interpreted as unsigned integer e' 23 bits Interpreted as a base 2 value defined as m' = 0.b1b2…b23 = b12-1 + b22-2 +…+ b232-23 if e' ≠ 0 then FP number = (-1)s × (1 + m') × 2e'-127 if e' = 0 then FP number = (-1)s × m' × 2-126 12
Example: IEEE 754 single precision (32-bit) 01010110010010100000000000000000 The more bits available, the more precise the mantissa and the larger the exponent range. See: https://en.wikipedia.org/wiki/IEEE_floating_point s = 0 e' = 17210 m' = 2-1 + 2-4 + 2-6 = 0.578125 Number = (-1)s × (1 + m') × 2e'-127 = 1.578125 × 245 ≈ 5.55253372027 × 1013 13