0% found this document useful (0 votes)
10 views14 pages

Lecture Slides 02 022-Integers

The document discusses the representation of integers and floating-point numbers in computing, focusing on unsigned and signed integers, including sign-and-magnitude and two's complement representations. It explains how arithmetic operations are performed on these representations and highlights the advantages of two's complement for simplifying arithmetic. Additionally, it covers the IEEE floating-point standard and its operations.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Lecture Slides 02 022-Integers

The document discusses the representation of integers and floating-point numbers in computing, focusing on unsigned and signed integers, including sign-and-magnitude and two's complement representations. It explains how arithmetic operations are performed on these representations and highlights the advantages of two's complement for simplifying arithmetic. Additionally, it covers the IEEE floating-point standard and its operations.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

University

 of  Washington  

Sec.on  2:  Integer  &  Floa.ng  Point  Numbers  


¢ Representa.on  of  integers:  unsigned  and  signed  
¢ Unsigned  and  signed  integers  in  C  
¢ Arithme.c  and  shiBing  
¢ Sign  extension  

¢ Background:  frac.onal  binary  numbers  


¢ IEEE  floa.ng-­‐point  standard  
¢ Floa.ng-­‐point  opera.ons  and  rounding  
¢ Floa.ng-­‐point  in  C  
 

Integers  
University  of  Washington  

Unsigned  Integers  
¢ Unsigned  values  are  just  what  you  expect  
§ b7b6b5b4b3b2b1b0  =  b727  +  b626  +  b525  +  …  +  b121  +  b020  
§ Useful  formula:    1+2+4+8+...+2N-­‐1  =  2N  -­‐1  
 
¢ You  add/subtract  them  using  the  normal    
“carry/borrow”  rules,  just  in  binary  
 
63 00111111
+ 8 +00001000
71 01000111

Integers  
University  of  Washington  

Signed  Integers  
¢ Let's  do  the  natural  thing  for  the  posi.ves  
§ They  correspond  to  the  unsigned  integers  of  the  same  value  
§ Example  (8  bits):  0x00  =  0,  0x01  =  1,  …,  0x7F  =  127  
¢ But,  we  need  to  let  about  half  of  them  be  nega.ve  
§ Use  the  high  order  bit  to  indicate  nega%ve:  call  it  the  “sign  bit”  
Call  this  a  “sign-­‐and-­‐magnitude”  representaQon  
§
§ Examples  (8  bits):    
§ 0x00  =  000000002  is  non-­‐negaQve,  because  the  sign  bit  is  0  
§ 0x7F  =  011111112  is  non-­‐negaQve  
§ 0x85  =  100001012  is  negaQve  
§ 0x80  =  100000002  is  negaQve…  

Integers  
University  of  Washington  

Sign-­‐and-­‐Magnitude  Nega.ves  
¢ How  should  we  represent  -­‐1  in  binary?  
§ Sign-­‐and-­‐magnitude:    100000012  
Use  the  MSB  for  +  or  -­‐,  and  the  other  bits  to  give  magnitude  
 

–7 +0
–6 1111 0000 +1

–5 1110 0001 +2
1101 0010
–4 +3
1100 0011

– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5

–1 1000 0111 +6
–0 +7
Integers  
University  of  Washington  

Sign-­‐and-­‐Magnitude  Nega.ves  
¢ How  should  we  represent  -­‐1  in  binary?  
§ Sign-­‐and-­‐magnitude:    100000012  
Use  the  MSB  for  +  or  -­‐,  and  the  other  bits  to  give  magnitude  
(Unfortunate  side  effect:  there  are  two  representaQons  of  0!)  

–7 +0
–6 1111 0000 +1

–5 1110 0001 +2
1101 0010
–4 +3
1100 0011

– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5

–1 1000 0111 +6
–0 +7
Integers  
University  of  Washington  

Sign-­‐and-­‐Magnitude  Nega.ves  
¢ How  should  we  represent  -­‐1  in  binary?  
§ Sign-­‐and-­‐magnitude:    100000012  
Use  the  MSB  for  +  or  -­‐,  and  the  other  bits  to  give  magnitude  
(Unfortunate  side  effect:  there  are  two  representaQons  of  0!)  
§ Another  problem:  math  is  cumbersome  
§ Example:   –7 +0
–6 1111 0000 +1
4  -­‐  3  !=  4  +  (-­‐3)  
–5 1110 0001 +2
1101 0010
0100 –4 +3
+1011 1100 0011
1111
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5

–1 1000 0111 +6
–0 +7
Integers  
University  of  Washington  

Two’s  Complement  Nega.ves  


¢ How  should  we  represent  -­‐1  in  binary?  
§ Rather  than  a  sign  bit,  let  MSB  have  same  value,  but  nega%ve  weight  
§ W-­‐bit  word:  Bits  0,  1,  …,  W-­‐2  add  20,  21,  …,  2W-­‐2  to  value  of  integer  
when  set,  but  bit  W-­‐1  adds  -­‐2W-­‐1  when  set  
§ e.g.  unsigned  10102:    1*23  +  0*22  +  1*21  +  0*20  =  1010  
 2’s  comp.  10102:  -­‐1*23  +  0*22  +  1*21  +  0*20  =  -­‐610  
§ So  -­‐1  represented  as  11112;  all   –1 0
negaQve  integers  sQll  have  MSB  =  1   –2 1111 0000 +1
§ Advantages  of  two’s  complement:   –3 1110 0001 +2
only  one  zero,  simple  arithmeQc   1101 0010
–4 +3
§ To  get  negaQve  representaQon  of   1100 0011
any  integer,  take  bitwise  complement  
– 5 1011 0100 + 4
and  then  add  one!  
1010 0101
~x + 1 = -x –6 1001 0110 +5

–7 1000 0111 +6
Integers   –8 +7
University  of  Washington  

Two’s  Complement  Arithme.c  


¢ The  same  addi.on  procedure  works  for  both  unsigned  and  
two’s  complement  integers  
§ Simplifies  hardware:  only  one  adder  needed  
§ Algorithm:  simple  addiQon,  discard  the  highest  carry  bit  
§ Called  “modular”  addiQon:  result  is  sum  modulo  2W  
¢ Examples:  

Integers  
University  of  Washington  

Two’s  Complement  
¢ Why  does  it  work?  
§ Put  another  way:  given  the  bit  representaQon  of  a  posiQve  integer,  we  
want  the  negaQve  bit  representaQon  to  always  sum  to  0  (ignoring  the  
carry-­‐out  bit)  when  added  to  the  posiQve  representaQon  
§ This  turns  out  to  be  the  bitwise  complement  plus  one  
§ What  should  the  8-­‐bit  representaQon  of  -­‐1  be?  
00000001
+????????    (we  want  whichever  bit  string  gives  the  right  result)  
00000000

00000010 00000011
+???????? +????????
00000000 00000000

Integers  
University  of  Washington  

Two’s  Complement  
¢ Why  does  it  work?  
§ Put  another  way:  given  the  bit  representaQon  of  a  posiQve  integer,  we  
want  the  negaQve  bit  representaQon  to  always  sum  to  0  (ignoring  the  
carry-­‐out  bit)  when  added  to  the  posiQve  representaQon  
§ This  turns  out  to  be  the  bitwise  complement  plus  one  
§ What  should  the  8-­‐bit  representaQon  of  -­‐1  be?  
00000001
+11111111    (we  want  whichever  bit  string  gives  the  right  result)  
100000000

00000010 00000011
+???????? +????????
00000000 00000000

Integers  
University  of  Washington  

Two’s  Complement  
¢ Why  does  it  work?  
§ Put  another  way:  given  the  bit  representaQon  of  a  posiQve  integer,  we  
want  the  negaQve  bit  representaQon  to  always  sum  to  0  (ignoring  the  
carry-­‐out  bit)  when  added  to  the  posiQve  representaQon  
§ This  turns  out  to  be  the  bitwise  complement  plus  one  
§ What  should  the  8-­‐bit  representaQon  of  -­‐1  be?  
00000001
+11111111    (we  want  whichever  bit  string  gives  the  right  result)  
100000000

00000010 00000011
+11111110 +11111101
100000000 100000000

Integers  
University  of  Washington  

Unsigned  &  Signed  Numeric  Values  


X   Unsigned   Signed  
l Both  signed  and  unsigned  integers    
0000   0   0  
have  limits  
0001   1   1   l If  you  compute  a  number  that  is  too  big,  
0010   2   2   you  wrap:    6  +  4  =  ?    15U  +  2U  =  ?  
0011   3   3  
If  you  compute  a  number  that  is  too  small,  
0100   4   4  
l

you  wrap:    -­‐7  -­‐  3  =  ?    0U  -­‐  2U  =  ?  


0101   5   5  
 
0110   6   6  
0111   7   7   l The  CPU  may  be  capable  of  “throwing  an  
1000   8   –8   excepQon”  for  overflow  on  signed  values  
1001   9   –7  
l But  it  won't  for  unsigned  
1010   10   –6  
 
1011   11   –5  
1100   12   –4   l C  and  Java  just  cruise  along  silently  when  
1101   13   –3   overflow  occurs...  
1110   14   –2  
1111   15   –1  

Integers  
University  of  Washington  

Visualiza.ons  
¢ Same  W  bits  interpreted  as  signed  vs.  unsigned:  
  Two’s" 2w" 2w" Two’s"
 
complement" complement"
+2w–1" 2w–1" 2w–1" +2w–1"
 
  0" 0" 0" 0"
  –2w–1" Unsigned" Unsigned"
–2w–1"
¢ Two’s  complement  (signed)  addi.on:  x  and  y  are  W  bits  wide  
x + y!
+2w" Positive overflow"

+2w –1" +2w –1"

0" Normal" 0"

–2w –1" –2w –1"


Negative overflow"
–2w"
Integers  
University  of  Washington  

Values  To  Remember  


¢ Unsigned  Values   ¢ Two’s  Complement  Values  
§ UMin  =  0   § TMin  =    –2w–1  
§ 000…0   § 100…0  
§ UMax    =    2w  –  1   § TMax    =    2w–1  –  1  
§ 111…1   § 011…1  
§ NegaQve  1  
§ 111…1        0xFFFFFFFF  (32  bits)  

Values  for  W  =  16  


Decimal Hex Binary
UMax 65535 FF FF 11111111 11111111
TMax 32767 7F FF 01111111 11111111
TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000
Integers  

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy