Information Encoding in Computer Organization and Design

undefined
 
Computer Organization and Design
Computer Organization and Design
Representing Operands
Representing Operands
 
Montek Singh
Montek Singh
Sep 6, 2017
Sep 6, 2017
 
Lecture 3
Lecture 3
 
1
 
Representing Operands
 
Characters
Integers
Positive numbers
Negative numbers
Non-Integers
Fixed-Point Numbers
Floating-Point Numbers
 
Reading:
Chapter 2.3-2.4
Chapter 3.5 (only through pg. 202)
 
2
 
Motivation
 
Computer use binary representation internally
Computer use binary representation internally
a wire is “hot” or “cold”
a wire is “hot” or “cold”
a 
a 
switch is “on” or “off”
switch is “on” or “off”
How do we use bits to represent 
How do we use bits to represent 
information
information
?
?
We need standards of representations for
We need standards of representations for
Letters
Letters
Numbers
Numbers
Colors/pixels
Colors/pixels
Music
Music
Video
Video
3
 
Information Encoding
 
Encoding = assign representation to information
Encoding = assign representation to information
Examples:
Examples:
suppose you have two “things” (symbols) to encode
suppose you have two “things” (symbols) to encode
one is 
one is 
 and other 
 and other 
what would you do?
what would you do?
now suppose you have 4 symbols to encode
now suppose you have 4 symbols to encode
, 
, 
, 
, 
 and 
 and 
what would you do?
what would you do?
now suppose you have the following numbers to encode
now suppose you have the following numbers to encode
1, 3, 5 and 7
1, 3, 5 and 7
what would you do?
what would you do?
4
 
Encoding is an art
 
Choosing an appropriate and efficient encoding is a
Choosing an appropriate and efficient encoding is a
real engineering challenge (and an art)
real engineering challenge (and an art)
 
Impacts design at many levels
Impacts design at many levels
Complexity (how hard to encode/decode)
Complexity (how hard to encode/decode)
Efficiency (#bits used, transmit energy)
Efficiency (#bits used, transmit energy)
Reliability (what happens with noise?)
Reliability (what happens with noise?)
Security (encryption)
Security (encryption)
 
5
 
Fixed-Length Encodings
 
What is fixed-length encoding?
What is fixed-length encoding?
all symbols are encoded using the same number of bits
all symbols are encoded using the same number of bits
When to use it?
When to use it?
if all symbols are equally likely (or we have no reason to
if all symbols are equally likely (or we have no reason to
expect otherwise)
expect otherwise)
 
When not to use it?
When not to use it?
when some symbols are more likely, while some are rare
when some symbols are more likely, while some are rare
what to use then:  variable-length encoding
what to use then:  variable-length encoding
example:
example:
suppose X is twice as likely as Y or Z
suppose X is twice as likely as Y or Z
how would we encode them?
how would we encode them?
6
 
Fixed-Length Encodings
 
Length of a fixed-length code
Length of a fixed-length code
use as many bits as needed to unambiguously represent all
use as many bits as needed to unambiguously represent all
symbols
symbols
1 bit suffices for 2 symbols
1 bit suffices for 2 symbols
2 bits suffice for …?
2 bits suffice for …?
n bits suffice for …?
n bits suffice for …?
how many bits needed for M symbols?
how many bits needed for M symbols?
ex. Decimal digits 10 = {0,1,2,3,4,5,6,7,8,9}
ex. Decimal digits 10 = {0,1,2,3,4,5,6,7,8,9}
4-bit binary code:  0000 to 1001
4-bit binary code:  0000 to 1001
 
ex. ~84 English characters = {A-Z (26), a-z (26), 0-9 (10),
ex. ~84 English characters = {A-Z (26), a-z (26), 0-9 (10),
punctuation (8), math (9), financial (5)}
punctuation (8), math (9), financial (5)}
7-bit ASCII (American Standard Code for Information Interchange)
7-bit ASCII (American Standard Code for Information Interchange)
7
 
Encoding Characters
 
ASCII Code:  use 7 bits to encode 128 characters
 
8
 
Encoding More Characters
ASCII is biased towards western languages, esp.
ASCII is biased towards western languages, esp.
English
English
In fact, many more than 256 chars in common use:
In fact, many more than 256 chars in common use:
 
 
â, m, ö, ñ, è, ¥, 
â, m, ö, ñ, è, ¥, 
, 
, 
, 
, 
, 
, 
, ℵ, ℷ, 
, ℵ, ℷ, 
ж
ж
, 
, 
Unicode is a worldwide standard that supports all
Unicode is a worldwide standard that supports all
languages, special characters, classic, and arcane
languages, special characters, classic, and arcane
Several encoding variants, e.g. 16-bit (UTF-8)
Several encoding variants, e.g. 16-bit (UTF-8)
9
 
Encoding Positive Integers
How to encode positive numbers in binary?
How to encode positive numbers in binary?
Each number is a sequence of 0s and 1s
Each number is a sequence of 0s and 1s
Each bit is assigned a weight
Each bit is assigned a weight
Weights are increasing powers of 2, right to left
Weights are increasing powers of 2, right to left
The value of an n-bit number is
The value of an n-bit number is
 
   2
4
 =      16
 
+ 2
8
 =    256
 
+ 2
6
 =      64
 
+ 2
7
 =    128
 
+ 2
9
 =    512
 
+ 2
10
 = 1024
10
10
 
Some Bit Tricks
 
Get used to working in binary
Get used to working in binary
Specifically for Comp 411, but it will be helpful throughout
Specifically for Comp 411, but it will be helpful throughout
your career as a computer scientist
your career as a computer scientist
Here are some helpful guides
Here are some helpful guides
 
1.
Memorize the first 10 powers of 2
 
2
0
 = 1
  
2
5
 = 32
2
1
 = 2
  
2
6
 = 64
2
2
 = 4
  
2
7
 = 128
2
3
 = 8
  
2
8
 = 256
2
4
 = 16
  
2
9
 = 512
 
11
11
 
More Tricks with Bits
 
Get used to working in binary
Get used to working in binary
Here are some helpful guides
Here are some helpful guides
 
2.   Memorize the prefixes for powers of 2 that are
multiples of 10
 
2
10
  = Kilo (1024)
2
20
  = Mega (1024*1024)
2
30
  = Giga (1024*1024*1024)
2
40
  = Tera (1024*1024*1024*1024)
2
50
  = Peta (1024*1024*1024*1024*1024)
2
60
  = Exa  (1024*1024*1024*1024*1024*1024)
 
For fun:  
http://highscalability.com/blog/2012/9/11/how-big-is-a-petabyte-exabyte-zettabyte-
or-a-yottabyte.html
 
12
12
 
Even More Tricks with Bits
 
Get used to working in binary
Get used to working in binary
Here are some helpful guides
Here are some helpful guides
 
3.
When you convert a binary number to
decimal, first break it down into clusters of
10 bits.
4.
Then compute the value of the leftmost
remaining bits (1) find the appropriate prefix
(GIGA) (Often this is sufficient)
5.
Compute the value of and add in each
remaining 10-bit cluster
0000101000
0000001100
0000000011
 
01
 
13
13
 
Other Helpful Clusterings
Sometimes convenient to use other number “bases”
Sometimes convenient to use other number “bases”
often bases are powers of 2:  e.g., 8, 16
often bases are powers of 2:  e.g., 8, 16
allows bits to be clustered into groups
allows bits to be clustered into groups
base 8 is called 
base 8 is called 
octal 
octal 
 
 
 groups of 3 bits
 groups of 3 bits
Convention:  lead the number with a 
Convention:  lead the number with a 
0
0
 
0
3720
Octal - base 8
000 - 0
001 - 1
010 - 2
011 - 3
100 - 4
101 - 5
110 - 6
111 - 7
 
= 2000
10
14
14
 
One Last Clustering
Base 16 is most common!
Base 16 is most common!
called 
called 
hexadecimal 
hexadecimal 
or 
or 
hex 
hex 
 
 
 groups of 
 groups of 
4 bits
4 bits
hex ‘digits’ (“hexits”):  0-9, and A-F
hex ‘digits’ (“hexits”):  0-9, and A-F
each hexit position represents a power of 16
each hexit position represents a power of 16
Convention:  lead with 
Convention:  lead with 
0x
0x
 
0x
7d0
Hexadecimal - base 16
0000 - 0   1000 - 8
0001 - 1     1001 - 9
0010 - 2    1010 - a
0011 - 3     1011 - b
0100 - 4     1100 - c
0101 - 5     1101 - d
0110 - 6     1110 - e
0111 - 7     1111 - f
 
= 2000
10
15
15
 
Signed-Number Representations
What about 
What about 
signed
signed
 numbers?
 numbers?
one obvious idea:  use an extra bit to encode the sign
one obvious idea:  use an extra bit to encode the sign
convention:  the most significant bit (leftmost) is used for the sign
convention:  the most significant bit (leftmost) is used for the sign
called the SIGNED MAGNITUDE representation
called the SIGNED MAGNITUDE representation
 
2000
16
16
 
Signed-Number Representations
 
The Good:  Easy to negate, find absolute value
The Bad:
add/subtract is complicated
depends on the signs
4 different cases!
two different ways of representing a 0
i
t is not used that frequently in practice
except in floating-point numbers
 
17
17
 
Alternative:  2
s Complement Rep.
2
0
2
1
2
2
2
3
2
N-2
-2
N-1
N bits
The 2’
s complement representation for signed integers is
the most commonly used signed-integer representation. It
is a simple modification of unsigned integers where the
most significant bit is considered 
negative.
Range: – 2
N-1
  to  2
N-1
 – 1
 
8-bit 2
s complement example:
    
11010110 = –2
7
 + 2
6
 + 2
4
 + 2
2
 + 2
1
  
= – 128 + 64 + 16 + 4 + 2 = – 42
18
18
 
Why 2
s Complement?
Benefit: the same binary addition (mod 2
Benefit: the same binary addition (mod 2
n
n
) procedure
) procedure
will work for adding positive and negative numbers
will work for adding positive and negative numbers
Don
Don
t need separate subtraction rules!
t need separate subtraction rules!
The same procedure will also handle unsigned numbers!
The same procedure will also handle unsigned numbers!
NOTE:  We typically ignore the leftmost carry
NOTE:  We typically ignore the leftmost carry
19
19
 
2
s Complement
How to negate a number?
How to negate a number?
First complement every bit (i.e. 1 
First complement every bit (i.e. 1 
 
 
0, 0 
0, 0 
 1), then add 1
 1), then add 1
4-bit example
4-bit example
+5
+5
 
 
= 0101  
= 0101  
 
 
 
 
 
 
-5 = 
-5 = 
 
 
1010 + 1 = 1011 = 1+2-8
1010 + 1 = 1011 = 1+2-8
-5 
-5 
 
 
= 1011  
= 1011  
  
  
 
 
+5 = 
+5 = 
 
 
0100 + 1 = 0101 = 1+4
0100 + 1 = 0101 = 1+4
8-bit example
8-bit example
+20
+20
 
 
= 00010100  
= 00010100  
 
 
 
 
 
 
-20 = 
-20 = 
 
 
11101011 + 1 = 11101100
11101011 + 1 = 11101100
-20 
-20 
 
 
= 11101100  
= 11101100  
  
  
 
 
+20 = 
+20 = 
 
 
00010011 + 1 = 00010100
00010011 + 1 = 00010100
Why does this work?
Why does this work?
Proof on board.  Hint:
Proof on board.  Hint:
20
20
 
2
s Complement
 
How to negate a number?
How to negate a number?
 
Method:
Method:
Complement every bit
Complement every bit
Add 1 to LSB
Add 1 to LSB
 
Shortcut
Shortcut
Keep the rightmost “1” and any following “0”s as they are
Keep the rightmost “1” and any following “0”s as they are
Complement all remaining bits
Complement all remaining bits
Example:  100
Example:  100
1000
1000
 
 
 011
 011
1000
1000
 
21
21
 
2
s Complement
Sign-Extension
Sign-Extension
suppose you have an 8-bit number that needs to be
suppose you have an 8-bit number that needs to be
“extended” to 16 bits
“extended” to 16 bits
Why?  Maybe because we are adding it to a 16-bit number…
Why?  Maybe because we are adding it to a 16-bit number…
Examples
Examples
16-bit version of 42  = 
16-bit version of 42  = 
0000 0000 
0000 0000 
0010 1010
0010 1010
8-bit version of -2  =                   1111 1110
8-bit version of -2  =                   1111 1110
Why does this work?
Why does this work?
Same hint:
Same hint:
Proof on board
Proof on board
22
22
 
Tutorial on Base Conversion 
(+ve ints)
Binary to Decimal
Binary to Decimal
multiply each bit by its positional power of 2
multiply each bit by its positional power of 2
add them together
add them together
Decimal to Binary
Decimal to Binary
Problem:  given 
Problem:  given 
v,
v,
 find 
 find 
b
b
i
i
 (inverse problem)
 (inverse problem)
Hint:  expand series
Hint:  expand series
observe: every term is even except first
observe: every term is even except first
this determines 
this determines 
b
b
0
0
divide both sides by 2
divide both sides by 2
23
23
 
Tutorial on Base Conversion 
(+ve ints)
 
Decimal to Binary
Decimal to Binary
Problem:  given 
Problem:  given 
v,
v,
 find 
 find 
b
b
i
i
 (inverse problem)
 (inverse problem)
 
 
Algorithm:
Algorithm:
Repeat
Repeat
divide 
divide 
v
v
 by 2
 by 2
remainder becomes the next bit, 
remainder becomes the next bit, 
b
b
i
i
quotient becomes the next 
quotient becomes the next 
v
v
Until 
Until 
v
v
 equals 0
 equals 0
 
Note:  Same algorithm applies to other number bases
Note:  Same algorithm applies to other number bases
just replace divide-by-2 by divide-by-
just replace divide-by-2 by divide-by-
n
n
 for base 
 for base 
n
n
24
24
 
Non-Integral Numbers
 
How about non-integers?
examples
1.234
-567.34
0.00001
0.0000000000000012
fixed-point representation
floating-point representation
25
25
 
Fixed-Point Representation
Set a definite position for the “binary” point
Set a definite position for the “binary” point
everything to its left is the integral part of the number
everything to its left is the integral part of the number
everything to its right is the fractional part of the number
everything to its right is the fractional part of the number
 
 
1101.0110
1101.0110
 
 
= 2
= 2
3
3
 + 2
 + 2
2
2
 + 2
 + 2
0
0
 + 2
 + 2
-2
-2
 + 2
 + 2
-3
-3
      
      
= 8 + 4 + 1 + 0.25 +
= 8 + 4 + 1 + 0.25 +
0.125
0.125
   
   
= 13.375
= 13.375
 
 
       Or
1101.0110      = 214 * 2
-4
 = 214/16 = 13.375
2
3
2
2
2
1
2
0
2
-1
2
-2
2
-3
2
-4
26
26
 
Fixed-Point Base Conversion
Binary to Decimal
multiply each bit by its positional power of 2
just that the powers of 2 are now negative
for m fractional bits
Examples
0.1
2
 = ½ = 0.5
ten
0.0011
2
 = 1/8 + 1/16 = 0.1875
ten
0.001100110011
2
= 1/8+1/16+1/128+1/256+1/2048+1/4096
= 0.19995117187
ten
 (getting close to 0.2)
0.0011
2
 (repeats) = 0.2
ten
27
27
 
Fixed-Point Base Conversion
 
Decimal to Binary
Decimal to Binary
Problem:  given 
Problem:  given 
v,
v,
 find 
 find 
b
b
i
i
 (inverse problem)
 (inverse problem)
 
 
Hint:  this time, try multiplying by 2
Hint:  this time, try multiplying by 2
 
 
whole number part is 
whole number part is 
b
b
-1
-1
remaining fractional part is the rest
remaining fractional part is the rest
Algorithm:
Algorithm:
Repeat
Repeat
multiply 
multiply 
v
v
 by 2
 by 2
whole part becomes the next bit, 
whole part becomes the next bit, 
b
b
i
i
remaining fractional part becomes the next 
remaining fractional part becomes the next 
v
v
Until (
Until (
v
v
 equals 0) or (desired accuracy is achieved)
 equals 0) or (desired accuracy is achieved)
28
28
 
Repeated Binary Fractions
 
Not all fractions have a finite representation
Not all fractions have a finite representation
e.g., in decimal, 1/3 = 0.3333333… (unending)
e.g., in decimal, 1/3 = 0.3333333… (unending)
In binary, many of the fractions you are used to have
In binary, many of the fractions you are used to have
an infinite representation!
an infinite representation!
Examples
Examples
1/10 = 0.1
1/10 = 0.1
10
10
 = 0.000110011…
 = 0.000110011…
2
2
=0.0
=0.0
0011
0011
2
2
1/5 = 0.2
1/5 = 0.2
10
10
 = 0.
 = 0.
0011
0011
2 
2 
= 0.333…
= 0.333…
16
16
 
Question
Question
In Decimal:  When do fractions repeat?
In Decimal:  When do fractions repeat?
when the denominator is mutually prime w.r.t. 5 and 2
when the denominator is mutually prime w.r.t. 5 and 2
In Binary:  When do fractions repeat?
In Binary:  When do fractions repeat?
when the denominator is mutually prime w.r.t. 2
when the denominator is mutually prime w.r.t. 2
i.e., when denominator is anything other than a power of 2
i.e., when denominator is anything other than a power of 2
29
29
 
Signed fixed-point numbers
 
How do you incorporate a sign?
use sign magnitude representation
an extra bit (leftmost) stores the sign
just as in negative integers
2’s complement
leftmost bit has a negative coefficient
 
 
 
1101.0110 = -2
3
 + 2
2
 + 2
0
 + 2
-2
 + 2
-3
  = -8 + 4 + 1 + 0.25 + 0.125 = -2.625
OR:
first ignore the binary point, use 2’s complement, put the point back
 
1101.0110  = (-128 + 64 + 16 + 4 + 2)* 2
-4
  
= -42/16 = -2.625
30
30
 
Signed fixed-point numbers
 
How to negate in 2’s complement representation
Same idea:  flip all the bits, and add “1” to the 
rightmost
 bit
not
 the bit to the left of the binary point
 
Example
 
1101.0110 = -2
3
 + 2
2
 + 2
0
 + 2
-2
 + 2
-3
  
= -8 + 4 + 1 + 0.25 + 0.125 = -2.625
 
 
1101.0110 
 0010.1001 + 0.0001 = 0010.1010
 
0010.1010  = 2
1
 + 2
-1
 + 2
-3
  
= 2 + 0.5 + 0.125 = 2.625
31
31
 
Bias Notation
Idea:  add a large number to everything, to make
Idea:  add a large number to everything, to make
everything look positive!
everything look positive!
must subtract this “bias” from every representation
must subtract this “bias” from every representation
This representation is called  
This representation is called  
Bias Notation
Bias Notation
.
.
1
1
0
1
0
1
1
0
2
0
2
5
2
4
2
3
2
2
2
1
2
6
2
7
 
Why?  Monotonicity
32
32
 
Floating-Point Representation
 
Another way to represent numbers is to use a
Another way to represent numbers is to use a
notation similar to Scientific Notation.
notation similar to Scientific Notation.
This format can be used to represent numbers with fractions
This format can be used to represent numbers with fractions
(3.90 x 10
(3.90 x 10
-4
-4
), very small numbers (1.60 x 10
), very small numbers (1.60 x 10
-19
-19
), and large
), and large
numbers (6.02 x 10
numbers (6.02 x 10
23
23
).
).
This notation uses two fields to represent each number. The
This notation uses two fields to represent each number. The
first part represents a normalized fraction (called the
first part represents a normalized fraction (called the
significand), and the second part represents the exponent
significand), and the second part represents the exponent
(i.e. the position of the 
(i.e. the position of the 
floating
floating
 binary point).
 binary point).
 
Normalized Fraction
 
“dynamic range”
 
“bits of accuracy”
 
Exponent
 
33
33
 
IEEE 754 Format
1
S
1
11
S
23
Significand
8
Exponent
Single-precision format
Double-precision format
IEEE 754 Floating-Point Formats
34
34
 
In Closing
 
Selecting encoding scheme has imp. implications
Selecting encoding scheme has imp. implications
how this information can be processed
how this information can be processed
how much space it requires.
how much space it requires.
Computer arithmetic is constrained by finite encoding
Computer arithmetic is constrained by finite encoding
Advantage:  it allows for complement arithmetic
Advantage:  it allows for complement arithmetic
Disadvantage:  it allows for overflows, numbers too big or
Disadvantage:  it allows for overflows, numbers too big or
small to be represented
small to be represented
Bit patterns can be interpreted in an endless number
Bit patterns can be interpreted in an endless number
of ways, however important standards do exist
of ways, however important standards do exist
Two
Two
s complement
s complement
IEEE 754 floating point
IEEE 754 floating point
 
35
35
Slide Note
Embed
Share

Discussions in this lecture delve into the importance and methodology of representing operands in computer systems, covering topics such as encoding characters, integers, positive and negative numbers, fixed-point and floating-point numbers. The motivation behind using binary representation is explored, highlighting the significance of standards in representing information like letters, numbers, colors, music, and video. The process of encoding information is considered an art, impacting design complexity, efficiency, reliability, and security at various levels. The concept of fixed-length encodings is also elucidated, emphasizing scenarios where it is advantageous and when variable-length encoding is more suitable.

  • Information Encoding
  • Computer Organization
  • Operand Representation
  • Binary Representation
  • Fixed-Length Encoding

Uploaded on Oct 05, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Computer Organization and Design Representing Operands Montek Singh Sep 6, 2017 Lecture 3 1

  2. Representing Operands Characters Integers Positive numbers Negative numbers Non-Integers Fixed-Point Numbers Floating-Point Numbers Reading: Chapter 2.3-2.4 Chapter 3.5 (only through pg. 202) 2

  3. Motivation Computer use binary representation internally a wire is hot or cold a switch is on or off How do we use bits to represent information? We need standards of representations for Letters Numbers Colors/pixels Music Video 3

  4. Information Encoding Encoding = assign representation to information Examples: suppose you have two things (symbols) to encode one is and other what would you do? now suppose you have 4 symbols to encode , , and what would you do? now suppose you have the following numbers to encode 1, 3, 5 and 7 what would you do? 4

  5. Encoding is an art Choosing an appropriate and efficient encoding is a real engineering challenge (and an art) Impacts design at many levels Complexity (how hard to encode/decode) Efficiency (#bits used, transmit energy) Reliability (what happens with noise?) Security (encryption) 5

  6. Fixed-Length Encodings What is fixed-length encoding? all symbols are encoded using the same number of bits When to use it? if all symbols are equally likely (or we have no reason to expect otherwise) When not to use it? when some symbols are more likely, while some are rare what to use then: variable-length encoding example: suppose X is twice as likely as Y or Z how would we encode them? 6

  7. Fixed-Length Encodings Length of a fixed-length code use as many bits as needed to unambiguously represent all symbols 1 bit suffices for 2 symbols 2 bits suffice for ? n bits suffice for ? how many bits needed for M symbols? ex. Decimal digits 10 = {0,1,2,3,4,5,6,7,8,9} 4-bit binary code: 0000 to 1001 log2(M) = 3.322 =4 bits log2(10) ex. ~84 English characters = {A-Z (26), a-z (26), 0-9 (10), punctuation (8), math (9), financial (5)} 7-bit ASCII (American Standard Code for Information Interchange) = 6.39 =7 bits log2(84) 7

  8. Encoding Characters ASCII Code: use 7 bits to encode 128 characters 8

  9. Encoding More Characters ASCII is biased towards western languages, esp. English In fact, many more than 256 chars in common use: , m, , , , , , , , , , , , Unicode is a worldwide standard that supports all languages, special characters, classic, and arcane Several encoding variants, e.g. 16-bit (UTF-8) 0xxxxxxx 1 0xxxxxx 1 0xxxxxx 1 0xxxxxx ASCII equiv range: 1 1 0y y y yx 1 0z y y y yx 1 0z y y y yx 16-bit Unicode 1 1 1 0z z z z 24-bit Unicode 1 1 1 1 0www 1 0wwz z z z 32-bit Unicode 9

  10. Encoding Positive Integers How to encode positive numbers in binary? Each number is a sequence of 0s and 1s Each bit is assigned a weight Weights are increasing powers of 2, right to left The value of an n-bit number is n-1 2ibi v = 21121029282726252423222120 0 1 1 1 1 1 0 1 0 0 0 0 i=0 24 = 16 + 26 = 64 + 27 = 128 + 28 = 256 + 29 = 512 + 210 = 1024 2000ten 10

  11. Some Bit Tricks Get used to working in binary Specifically for Comp 411, but it will be helpful throughout your career as a computer scientist Here are some helpful guides 1. Memorize the first 10 powers of 2 20 = 1 21 = 2 22 = 4 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 28 = 256 29 = 512 11

  12. More Tricks with Bits Get used to working in binary Here are some helpful guides 2. Memorize the prefixes for powers of 2 that are multiples of 10 210 = Kilo (1024) 220 = Mega (1024*1024) 230 = Giga (1024*1024*1024) 240 = Tera (1024*1024*1024*1024) 250 = Peta (1024*1024*1024*1024*1024) 260 = Exa (1024*1024*1024*1024*1024*1024) For fun: http://highscalability.com/blog/2012/9/11/how-big-is-a-petabyte-exabyte-zettabyte- or-a-yottabyte.html 12

  13. Even More Tricks with Bits Get used to working in binary Here are some helpful guides 0000000011 0000001100 0000101000 01 3. When you convert a binary number to decimal, first break it down into clusters of 10 bits. 4. Then compute the value of the leftmost remaining bits (1) find the appropriate prefix (GIGA) (Often this is sufficient) 5. Compute the value of and add in each remaining 10-bit cluster 13

  14. Other Helpful Clusterings Sometimes convenient to use other number bases often bases are powers of 2: e.g., 8, 16 allows bits to be clustered into groups base 8 is called octal groups of 3 bits Convention: lead the number with a 0 21121029282726252423222120 0 1 1 1 1 1 0 1 0000 n-1 = 200010 8idi v = i=0 3 7 2 0 03720 Octal - base 8 000 - 0 001 - 1 010 - 2 011 - 3 100 - 4 101 - 5 110 - 6 111 - 7 0*80 = 0 + 2*81 = 16 + 7*82 = 448 + 3*83 = 1536 200010 14

  15. One Last Clustering Base 16 is most common! called hexadecimal or hex groups of 4 bits hex digits ( hexits ): 0-9, and A-F each hexit position represents a power of 16 Convention: lead with 0x n-1 21121029282726252423222120 0 1 1 1 1 1 0 1 0000 16idi v = = 200010 i=0 0x7d0 7 d 0 Hexadecimal - base 16 0000 - 0 1000 - 8 0001 - 1 1001 - 9 0010 - 2 1010 - a 0011 - 3 1011 - b 0100 - 4 1100 - c 0101 - 5 1101 - d 0110 - 6 1110 - e 0111 - 7 1111 - f 0*160 = 0 + 13*161 = 208 + 7*162 = 1792 200010 15

  16. Signed-Number Representations What about signed numbers? one obvious idea: use an extra bit to encode the sign convention: the most significant bit (leftmost) is used for the sign called the SIGNED MAGNITUDE representation S 21029282726252423222120 0 1 1 1 1 1 0 1 0000 1 n-2 v =-1S 2ibi i=0 2000 -2000 16

  17. Signed-Number Representations The Good: Easy to negate, find absolute value The Bad: add/subtract is complicated depends on the signs 4 different cases! two different ways of representing a 0 it is not used that frequently in practice except in floating-point numbers 17

  18. Alternative: 2s Complement Rep. N bits -2N-1 2N-2 23 22 21 20 Range: 2N-1 to 2N-1 1 sign bit The 2 s complement representation for signed integers is the most commonly used signed-integer representation. It is a simple modification of unsigned integers where the most significant bit is considered negative. n-2 v =-2n-1bn-1+ 2ibi i=0 8-bit 2 s complement example: chute ladders 11010110 = 27 + 26 + 24 + 22 + 21 = 128 + 64 + 16 + 4 + 2 = 42 18

  19. Why 2s Complement? Benefit: the same binary addition (mod 2n) procedure will work for adding positive and negative numbers Don t need separate subtraction rules! The same procedure will also handle unsigned numbers! NOTE: We typically ignore the leftmost carry Example: 5510 = 001101112 + 1010 = 000010102 6510 = 010000012 5510 = + -1010 = 111101102 4510 = 1001011012 a 2 s complement representation. When using signed magnitude representations, adding a negative value really means to subtract a positive value. However, in 2 s complement, adding is adding regardless of sign. In fact, you NEVER need to subtract when you use 001101112 19

  20. 2s Complement How to negate a number? First complement every bit (i.e. 1 0, 0 1), then add 1 4-bit example +5 = 0101 -5 = -5 = 1011 +5 = 1010 + 1 = 1011 = 1+2-8 0100 + 1 = 0101 = 1+4 8-bit example +20 = 00010100 -20 = -20 = 11101100 +20 = 00010011 + 1 = 00010100 11101011 + 1 = 11101100 Why does this work? Proof on board. Hint: 11112=1+2+4+8=16-1= 24-1 n-1 =2n-1 2ibi i=0 20

  21. 2s Complement How to negate a number? Method: Complement every bit Add 1 to LSB Shortcut Keep the rightmost 1 and any following 0 s as they are Complement all remaining bits Example: 1001000 0111000 21

  22. 2s Complement Sign-Extension suppose you have an 8-bit number that needs to be extended to 16 bits Why? Maybe because we are adding it to a 16-bit number Examples 16-bit version of 42 = 0000 0000 0010 1010 8-bit version of -2 = 1111 1110 1111 1111 Why does this work? Same hint: Proof on board n-1 =2n-1 2ibi i=0 22

  23. Tutorial on Base Conversion (+ve ints) Binary to Decimal multiply each bit by its positional power of 2 add them together n-1 2ibi v = i=0 Decimal to Binary Problem: given v, find bi (inverse problem) Hint: expand series v=b0+2b1+4b2+8b3+...+2n-1bn-1 n-1 2ibi v = i=0 observe: every term is even except first this determines b0 divide both sides by 2 v div 2=b1+2b2+4b3+...+2n-2bn-1 (quotient) v mod 2=b0 (remainder) 23

  24. Tutorial on Base Conversion (+ve ints) Decimal to Binary Problem: given v, find bi (inverse problem) v=b0+2b1+4b2+8b3+...+2n-1bn-1 Algorithm: Repeat divide v by 2 remainder becomes the next bit, bi quotient becomes the next v Until v equals 0 Note: Same algorithm applies to other number bases just replace divide-by-2 by divide-by-n for base n 24

  25. Non-Integral Numbers How about non-integers? examples 1.234 -567.34 0.00001 0.0000000000000012 fixed-point representation floating-point representation 25

  26. Fixed-Point Representation Set a definite position for the binary point everything to its left is the integral part of the number everything to its right is the fractional part of the number 232221202-12-22-32-4 0.125 1101.0110 = 23 + 22 + 20 + 2-2 + 2-3 = 8 + 4 + 1 + 0.25 + = 13.375 Or 1101.0110 = 214 * 2-4 = 214/16 = 13.375 26

  27. Fixed-Point Base Conversion Binary to Decimal multiply each bit by its positional power of 2 just that the powers of 2 are now negative for m fractional bits v=b-1 2 4 8 v=2-1b-1+2-2b-2+2-3b-3+2-4b-4+...+2-mb-m -m v = 2ibi i=-1 +b-2 +b-3 +b-4 16+...+b-m 2m Examples 0.12 = = 0.5ten 0.00112 = 1/8 + 1/16 = 0.1875ten 0.0011001100112= 1/8+1/16+1/128+1/256+1/2048+1/4096 = 0.19995117187ten (getting close to 0.2) 0.00112 (repeats) = 0.2ten 27

  28. Fixed-Point Base Conversion Decimal to Binary Problem: given v, find bi (inverse problem) v=2-1b-1+2-2b-2+2-3b-3+2-4b-4+...+2-mb-m Hint: this time, try multiplying by 2 2v=b-1+2-1b-2+2-2b-3+2-3b-4+...+2-m+1b-m whole number part is b-1 remaining fractional part is the rest Algorithm: Repeat multiply v by 2 whole part becomes the next bit, bi remaining fractional part becomes the next v Until (v equals 0) or (desired accuracy is achieved) 28

  29. Repeated Binary Fractions Not all fractions have a finite representation e.g., in decimal, 1/3 = 0.3333333 (unending) In binary, many of the fractions you are used to have an infinite representation! Examples 1/10 = 0.110 = 0.000110011 2=0.000112 1/5 = 0.210 = 0.00112 = 0.333 16 Question In Decimal: When do fractions repeat? when the denominator is mutually prime w.r.t. 5 and 2 In Binary: When do fractions repeat? when the denominator is mutually prime w.r.t. 2 i.e., when denominator is anything other than a power of 2 29

  30. Signed fixed-point numbers How do you incorporate a sign? use sign magnitude representation an extra bit (leftmost) stores the sign just as in negative integers 2 s complement leftmost bit has a negative coefficient -232221202-12-22-32-4 1101.0110 = -23 + 22 + 20 + 2-2 + 2-3 = -8 + 4 + 1 + 0.25 + 0.125 = -2.625 OR: first ignore the binary point, use 2 s complement, put the point back 1101.0110 = (-128 + 64 + 16 + 4 + 2)* 2-4 = -42/16 = -2.625 30

  31. Signed fixed-point numbers How to negate in 2 s complement representation Same idea: flip all the bits, and add 1 to the rightmost bit not the bit to the left of the binary point Example 1101.0110 = -23 + 22 + 20 + 2-2 + 2-3 = -8 + 4 + 1 + 0.25 + 0.125 = -2.625 1101.0110 0010.1001 + 0.0001 = 0010.1010 0010.1010 = 21 + 2-1 + 2-3 = 2 + 0.5 + 0.125 = 2.625 31

  32. Bias Notation Idea: add a large number to everything, to make everything look positive! must subtract this bias from every representation This representation is called Bias Notation . n-1 27 26 2524232221 20 2ibi v = -Bias 1 1 0 1 0 1 1 0 i=0 6 * 1 = 6 13 * 16 = 208 - 127 87 Ex: (Bias = 127) Why? Monotonicity 32

  33. Floating-Point Representation Another way to represent numbers is to use a notation similar to Scientific Notation. This format can be used to represent numbers with fractions (3.90 x 10-4), very small numbers (1.60 x 10-19), and large numbers (6.02 x 1023). This notation uses two fields to represent each number. The first part represents a normalized fraction (called the significand), and the second part represents the exponent (i.e. the position of the floating binary point). Exponent 2 Normalized Fraction Exponent Normalized Fraction dynamic range bits of accuracy 33

  34. IEEE 754 Floating-Point Formats IEEE 754 Format The 1 is hidden because it provides no information after the number is normalized This is effectively a signed magnitude fixed-point number with a hidden 1. Single-precision format Significand S Exponent 23 1 8 The exponent is represented in bias 127 notation. Why? v=-1S 1.Significand 2Exponent-127 Double-precision format S Exponent Significand 1 11 52 v=-1S 1.Significand 2Exponent-1023 34

  35. In Closing Selecting encoding scheme has imp. implications how this information can be processed how much space it requires. Computer arithmetic is constrained by finite encoding Advantage: it allows for complement arithmetic Disadvantage: it allows for overflows, numbers too big or small to be represented Bit patterns can be interpreted in an endless number of ways, however important standards do exist Two s complement IEEE 754 floating point 35

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#