Notes 9
Notes 9
• Why represent information in computers using binary data? Why not natural base-10?
• (1) Because binary data is robust in memory
‣ magnetic polarity in hard disks
‣ electrical charge in flash memory
‣ electrical voltage in RAM memory
‣ electrical voltage in CPU chips
‣ pulses of light in optical fibers
• (2) Boolean logic is simple for implementation in hardware
‣ arithmetic for boolean number is much simpler than base-10 arithmetic
‣ can be implemented using simple boolean logic gates
‣ examples: AND, OR, XOR, NOT, ...
‣ logic gates are easy to make on integrated circuit chips (and cheap!)
‣ we shall see how to perform binary arithmetic later . . .
3. Binary Arithmetic
• Lets practice with binary addition
• Consider the following example:
1010
+0100
______
1110
• Now an example with carries
! 1101
! +0101
! ______
! 10010
• Exactly the same as decimal arithmetic (grade 3?)
• How does the computer CPU chip implement binary addition?
A B
1-bit!
Cout Full! Cin
Adder
• Using boolean logic circuits (note the transition from arithmetic to logic)
• Here is the logic circuit for a 1-bit adder
• see: http://www.circuitstoday.com/half-adder-and-full-adder
• Logic:
Carry = AND(A, B)
Sum = ExclusiveOR(A, B)
• Example: show sum and carry logic for binary addition
• Above is actually called a "half-adder" because it has no input for the carry bit. Need to use
two half-adders to make a "full-adder" for each bit in the binary number.
• Here is a full-adder circuit (for curiosity sake only):
A
B S
Cin
Cout
S3 S2 S1 S0
• Advantages
‣ testing for negative value is easy (high order bit = 1)
‣ for positive numbers, the signed and unsigned versions are the same
‣ only one representation for zero
‣ addition and subtraction work the same (without testing the sign bit)
• Example: signed addition
! 1011!! = -5
! 0100!! = +4
! ____
! 1111!! = -1
• which can be seen by converting back to unsigned
! 1111
! -1 # subtract 1
! ____
! 1110 ! # now flip bits
! 0001 ! = 1
6. Floating-Point Numbers
• Floating point numbers are a different "kettle of fish"
• What are they for?
• Compare: counting versus distance
• Counting uses integers
• Distance measure is a real value (arbitrarily small or large)
• Approximated in computer arithmetic using floating point representation
• Basic Idea: Use scientific notation to extend dynamic range
• Examples in decimal:
0.67788 * 1028! ! # large number
0.1 * 10-200! ! ! # very small number
• Scientific notation represents real numbers as:
(<exp>, <fraction>)
where <exp> is a signed integer
and <fraction> is a signed integer fraction with the radix
point at the far left
• Unicode
‣ Successor to ASCII
‣ contains as a subset
‣ used by Java exclusively
‣ 16-bit code
‣ supports many languages / character sets