Binary Representations

Download Report

Transcript Binary Representations

Computer Science 101
Binary Systems
Humans

Decimal Numbers (base 10)

Sign-Magnitude (-324)

Decimal Fractions (23.27)

Letters for text
Computers

Binary Numbers (base 2)

Two’s complement and sign-magnitude

Binary fractions and floating point

ASCII codes for characters (A65)
Why binary?

Information is stored in computer via voltage
levels.

Using decimal would require 10 distinct and
reliable levels for each digit.

This is not feasible with reasonable reliability and
financial constraints.

Everything in computer is stored using binary:
numbers, text, programs, pictures, sounds, videos,
...
How can that be?
Everything in computer is
numbers, text, programs,
...
 Everything in computer is
numbers, text, programs,
...
 Everything in computer is
numbers, text, programs,
...
 Everything in computer is
numbers, text, programs,
...

stored using binary:
pictures, sounds, videos,
stored using binary:
pictures, sounds, videos,
stored using binary:
pictures, sounds, videos,
stored using binary:
pictures, sounds, videos,
Transistor

A transistor is an electronic switch

Basic unit of modern computer storage

Two steady states based on voltage levels

Say, 500 million transistors on a chip 1 cm2

Change states in billionth of sec

Solid state
Morse Code
Morse Code Tree
Decimal: Non-negatives

Base 10

Uses decimal digits: 0,1,2,3,4,5,6,7,8,9

Positional System - position gives power of
the base

Example:
3845 = 3x103 + 8x102 + 4x101 + 5x100

Positions:
…543210
Binary: Non-negatives

Base 2

Uses binary digits (bits): 0,1

Positional system

Example:
1101 = 1x23 + 1x22 + 0x21 + 1x20
Conversions

External
(Human)
25
A
Internal
(Computer)
11001
01000001

Humans want to see and enter numbers in
decimal.

Computers must store and compute with
bits.
Binary to Decimal Conversion

Algorithm:
• Expand binary number using positional
scheme.
• Perform computation using decimal arithmetic.

Example:
110012  1x24 + 1x23 + 0x22 + 0x21 + 1x20
= 24 + 23 + 20
= 16 + 8 + 1
= 2510
Decimal to Binary - Algorithm 1

Algorithm:
While N  0 do
Set N to N/2 (whole part)
Record the remainder (1 or 0)
end-of-loop
Set A to remainders in reverse order
Decimal to binary - Example
Example: Convert 32410 to binary
N
Rem
N Rem
324
162
0
5
0
81
0
2
1
40
1
1
0
20
0
0
1
10
0
 32410 = 1010001002

Decimal to Binary - Algorithm 2

Algorithm:
Set A to 0 (all bits 0)
While N  0 do
Find largest P with 2P  N
Set bit in position P of A to 1
Set N to N - 2P
end-of-loop
Decimal to binary - Example

Example: Convert 32410 to binary
N
Power
P
A
324
68
4
0

256
64
4
32410 = 1010001002
8
6
2
100000000
101000000
101000100
Binary Addition

One bit numbers:
+
0
1
0 | 0
1
1 | 1 10

Example
1111 1
110101 (53)
+ 101101 (45)
1100010 (98)
Octal Numbers

Base 8
Digits 0,1,2,3,4,5,6,7

Number does not have so many digits as
binary

Easy to convert to and from binary

Often used by people who need to see the
internal representation of data, programs,
etc.
Octal Conversions

Octal to Binary
• Simply convert each octal digit to a three bit
binary number.
• Example:
5368 = 101 011 1102

Binary to Octal
• Starting at right, group into 3 bit sections
• Convert each group to an octal digit
• Example
110111111010102 = 011 011 111 101 010
= 337528
Hexadecimal

Base 16
Digits 0,…,9,A,B,C,D,E,F

Hexadecimal  Binary
• Just like Octal, only use 4 bits per digit.

Example:
98C316 = 1001 1000 1100 00112

Example
110100111010112 = 0011 0100 1110 1011
= 34EB
Python example