03 Integers in C

background image

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  in  C  

background image

University  of  Washington  

¢

C  Programming  

§

#include  <limits.h>  

§

Declares  constants,  e.g.:  

§

ULONG_MAX  

§

LONG_MAX  

§

LONG_MIN  

§

Values  are  plaGorm  specific  

§

See:  /usr/include/limits.h  on  

Linux  

Values  for  Different  Word  Sizes  

Integers  in  C  

 

W  

 

8  

16  

32  

64  

UMax  

255  

65,535  

4,294,967,295  

18,446,744,073,709,551,615  

TMax  

127  

32,767  

2,147,483,647  

9,223,372,036,854,775,807  

TMin  

-­‐128  

-­‐32,768  

-­‐2,147,483,648  

-­‐9,223,372,036,854,775,808  

 
 

¢

Observa.ons  

§

|TMin  |    =    TMax  +  1  

§ 

Asymmetric  range  

§

UMax  =  2  *  TMax  +  1  

background image

University  of  Washington  

Signed  vs.  Unsigned  in  C  

¢

Constants  

§

By  default  are  considered  to  be  signed  integers  

§

Use  “U”  suffix  to  force  unsigned:  

§ 

0U,  4294967259U

Integers  in  C  

background image

University  of  Washington  

Signed  vs.  Unsigned  in  C  

¢

Cas.ng  

§ 

int tx, ty;

§ 

unsigned ux, uy;

§

Explicit  casXng  between  signed  &  unsigned:  

§ 

tx = (int) ux;

§ 

uy = (unsigned) ty;

§

Implicit  casXng  also  occurs  via  assignments  and  funcXon  calls:  

§ 

tx = ux;

§ 

uy = ty;  

§ 

The  gcc  flag  

-­‐Wsign-­‐conversion

 produces  warnings  for  implicit  casts,  

but  

-­‐Wall

 does  not!  

§

How  does  casXng  between  signed  and  unsigned  work  –  what  values  are  

going  to  be  produced?  

§

Bits  are  unchanged

,  just  interpreted  differently!  

Integers  in  C  

background image

University  of  Washington  

0

0U

==

unsigned

-1

0

<

signed

-1

0U

>

unsigned

2147483647

-2147483648

>

signed  

2147483647U

-2147483648

<

unsigned

-1

-2

>

signed

(unsigned) -1

-2

>

unsigned

2147483647

2147483648U

<

unsigned

2147483647

(int) 2147483648U

>

signed

Cas.ng  Surprises  

Integers  in  C  

¢

Expression  Evalua.on  

§

If  you  mix  unsigned  and  signed  in  a  single  expression,  then  

signed  values  implicitly  cast  to  unsigned  

§

Including  comparison  operaXons  <,  >,  ==,  <=,  >=

§

Examples  for  W  =  32:        

TMIN  =  -­‐2,147,483,648              TMAX  =  2,147,483,647  

¢

Constant

1

 Constant

2

 Rela.on  Evalua.on  

 0

 0U

   

 -­‐1

 0

   

 -­‐1

 0U

   

 2147483647

 -­‐2147483648  

   

 2147483647U

 -­‐2147483648

   

 -­‐1

 -­‐2  

   

 (unsigned)-­‐1

 -­‐2  

   

   2147483647  

 2147483648U  

   

   2147483647  

 (int)  2147483648U  


Wyszukiwarka

Podobne podstrony:
03 Integers in C
03 3 sitowa i in
03 Galaxy in Flames
MacGregor, Kinley (aka Sherrilyn Kenyon) Brotherhood 03 Born in Sin rtf
Diana Palmer Men of Medicine Ridge 03 Diamond in the Rough
Bova, Ben Orion 03 Orion in the Dying Time
Stasheff, Christopher Rogue Wizard 03 A Wizard in War
Nora Roberts Concannon Sisters Trilogy 03 Born In Shame
Evan Innes America 2040 Book 03 City In The Mist
Gardner, Craig Shaw Ebenezum 03 A Night in the Netherhells
Baker, Kage Company 03 Mendoza in Hollywood 1 0
MacAlister 03 Born in Sin
Norton, Andre Free Trader Moon Singer 03 Flight in Yiktor
Eliza Knight [Highland Jewel 03] Love In A Box ([df)(1)
Hugh Zachary America 2040 03 City In the Mist
MacGregor, Kinley (aka Sherrilyn Kenyon) MacAlister 03 Born in Sin
Roberts, Nora Born 03 Born In Shame

więcej podobnych podstron