Transcript Numbers

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
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
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
2
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.
bn-1bn-2…b1b0
MSB
= bn-12n-1 + bn-22n-2 + … + b121 + b020
LSB
3
Binary Numbers
Table 1: Given n bits, the number of possible states = 2n
n
# States
n
# States
n
# States
0
none
10
1,024
20
1,048,576
1
2
11
2,048
21
2,097,152
2
4
12
4,096
22
4,194,304
3
8
13
8,192
23
8,388,608
4
16
14
16,384
24
16,777,216
5
32
15
32,768
:
:
6
64
16
65,536
30
1,073,741,824
7
128
17
13,1072
31
2,147,483,648
8
256
18
26,2144
32
4,294,967,296
9
512
19
52,4288
64 18446744073709551616
4
Number Conversion Decimal - Binary

Convert from binary to its equivalent base 10 value
 Expand the powers of two
Example: What is 11102 in decimal?
11102
= (123) + (122) + (121) + (020)
= (18) + (14) + (12) + (01)
= 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
0
7/2=3
1
3/2=1
1
1/2=0
1
Read the remainders
backwards. Hence,
1410 = 11102
5
Bitwise Operations
A B
A&B
A B
A|B
A B
A^B
0 0
0
0 0
0
0 0
0
A
~A
0 1
0
0 1
1
0 1
1
0
1
1 0
0
1 0
1
1 0
1
1
0
1 1
1
1 1
1
1 1
0
Bitwise
Complement
Bitwise
AND
Bitwise
OR
A B
A+B
Carry
0 0
0
0
0 1
1
0
1 0
1
0
1 1
0
1
Bitwise
XOR
Bitwise Addition
6
Logic Operations
 Logic operations are done one bit at a time (unary,
AKA monadic) or a pair of bits (binary, AKA dyadic)
Example:
~1011 = 0100
1010 & 1100 = 1000
1010 | 1100 = 1110
1010 ^ 1100 = 0110
Complement, unary
Bitwise AND, binary
Bitwise OR
Bitwise XOR
7
Other Base Representations


Octal (base 8  0, …, 7)
Hexadecimal (base 16  0, …, 9, A, B, C, D, E, F with F
representing 1510)
Table 2: 4-bit positive integer conversion table
Dec
Bin
Oct
Hex
Dec
Bin
Oct
Hex
0
0000
0
0
8
1000
10
8
1
0001
1
1
9
1001
11
9
2
0010
2
2
10
1010
12
A
3
0011
3
3
11
1011
13
B
4
0100
4
4
12
1100
14
C
5
0101
5
5
13
1101
15
D
6
0110
6
6
14
1110
16
E
7
0111
7
7
15
1111
17
F
8
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:
4410010 = 10101100010001002 = AC4416

Converting from hex to its equivalent binary:
Replace each hex value by its 4-bit binary value.
Example:
2741110 = 6B1316 = 01101011000100112
9
Positive and Negative Integers

Integers are exactly and easily representable in any
integer base > 1, including base 2
Table 3: 4-bit positive only values 0 to 15
Base 10
Base 2
Base 10
Base 2
0
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
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
10


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 10
Base 2
(2's comp)
Base 10
Base 2
(2's comp)
0
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
If both positive and
negative integers are
needed, the maximum
positive value is reduced
by a factor of 2
Given : n bits
Range: –2n-1 to 2n-1 – 1
11
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)
mantissa
exponent
–6.349 × 102
sign
base
12

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
13

Example: IEEE 754 single precision (32-bit)
01010110010010100000000000000000
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

The more bits available, the more precise the
mantissa and the larger the exponent range
14